How to read from a text file?

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
 
A

Albert D. Kallal

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
 
L

Len Cuff

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top