Talk About Network

Google





Software > Access External data > Re: How to read...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 4 Topic 11283 of 11376
Post > Topic >>

Re: How to read from a text file?

by "Albert D. Kallal" <PleaseNOOOsPAMmkallal@[EMAIL PROTECTED] > Nov 22, 2008 at 02:27 PM

The code to direct open a text file looks like:

Sub ReadTextFile

    Dim strFile      As String
    Dim intF         As Integer
    Dim strLineBuf1  As String
    Dim strLineBuf2  As String

    strFile = "c:\my data\MyData.txt"

    intF = FreeFile()
    Open strFile For Input As #intF
    Line Input #intF, strLineBuf1
    Line Input #intF, strLineBuf2

    Close intF

So, buf1, and buf2 will have the 1st two lines from that text file.....

me.MyTextBoxOnForm = strLineBuf1 & strLineBuf2

If you want a carriage return between the two lines then go

me.MyTextBoxOnForm = strLineBuf1 & vbcrLf & strLineBuf2

If you want to read/process a whole text file and not know how many lines,

then a typical processing loop looks like:

Sub ReadTextFile

    Dim strFile      As String
    Dim intF         As Integer
    Dim strLineBuf   As String
    Dim lngLines     As Long
    Dim lngBlank     As Long

   strFile = "c:\my data\MyData.txt"

      intF = FreeFile()
      Open strFile For Input As #intF

      Do While EOF(intF) = False
         Line Input #intF, strLineBuf
         If Trim(strLineBuf) = "" Then
            lngBlank = lngBlank + 1
         Else
            lngLines = lngLines + 1
         End If
      Loop
      Close intF

   End If

   MsgBox "Number non blank lines  = " & lngLines & vbCrLf & _
          "Blank lines = " & lngBlank & vbCrLf & _
          "Total = " & lngBlank + lngLines

End Function

-- 
Albert D. Kallal    (Access MVP)
Edmonton, Alberta Canada
pleaseNOOSpamKallal@[EMAIL PROTECTED]

 




 4 Posts in Topic:
How to read from a text file?
Len Cuff <len.cuff@[EM  2008-11-22 19:20:21 
Re: How to read from a text file?
"Albert D. Kallal&qu  2008-11-22 14:27:52 
Re: How to read from a text file?
Len Cuff <len.cuff@[EM  2008-11-22 21:50:04 
Re: How to read from a text file?
"richard" <B  2008-11-28 13:38:36 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
localhost-V2008-12-19 Fri Jan 9 0:16:39 PST 2009.