How to read from a text file?

  • Thread starter Thread starter Len Cuff
  • Start date Start date
L

Len Cuff

Can anyone tellme how to read from a text file into a field on an
access form please.
I'm writing to a text file fine to log certain activity but need to
populate a field with the data held in a text file - only 2 lines.

Tia

cheers,
Len
 
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
 
On Sat, 22 Nov 2008 14:27:52 -0700, "Albert D. Kallal"


Many thanks, just what I wanted !

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


cheers,
Len
 
Back
Top