Click to See Complete Forum and Search --> : Reading/Parsing text files


enigmaos
September 6th, 2001, 03:29 PM
I can't figure out why this code doesn't display the whole text file. There are 86 numbers in that text file. For example,

11111
22222
33333
44444
55555
.
.
.

Only the first 13 numbers are displayed in the textbox, but the rest are just blanks, Chr(13), and Chr(10).

Does any of you know why it's doing that?

THANKS!!!!


Dim FileNum as string, strText as string, tmpPath as string
Dim i as Integer, EndPos as Integer, tmp as Integer

tmpPath = "C:\My Documents\Test.txt"
FileNum = FreeFile

Open tmpPath for input as FileNum
strText = input$(LOF(FileNum), #FileNum)
Close #FileNum

'### Chr(10) and Chr(13) are seperators ###
strText = Replace(strText, Chr(10), "")
i = InStr(1, strText, Chr(13))

EndPos = 1
tmp = i
'### Display every number ###
Do While i > 0
me.Text1.Text = Text1.Text & Chr(10) & mid(strText, EndPos, tmp)
EndPos = EndPos + i
i = InStr(i + 1, strText, Chr(13))
Loop





enigmaos@yahoo.com

DSJ
September 6th, 2001, 03:41 PM
Not sure why your's isn't working but set a reference to Microsoft Scripting Runtime (scrrun.dll) and try this code:


option Explicit

private Sub Command1_Click()
Dim fs as FileSystemObject
Dim ts as TextStream

set fs = new FileSystemObject
set ts = fs.OpenTextFile("C:\My Documents\Test.txt", ForReading)

Text1.Text = ""
Do While Not ts.AtEndOfStream
Text1.Text = Text1.Text + ts.ReadLine + vbCrLf
Loop
ts.Close
set ts = nothing
set fs = nothing

End Sub

enigmaos
September 6th, 2001, 03:54 PM
Thanks!

enigmaos@yahoo.com

John G Duffy
September 6th, 2001, 04:43 PM
Not quite sure why you ar going to such pains to read a text file into a textbox. Here is a much simpler method

' Read a file into a TEXT box using binary read
' this reduces the exposure of VB editing double quote marks and
' commas out of the text

private Sub Command1_Click()
Dim a
a = "C:\to DO LIST\1.txt"
Open a for binary as #1
Text1.Text = input(FileLen(a), #1)
Close

End Sub




John G