hi,

I want to ask about read data from .txt file and show them all to texbox in VB6
i just can do it for 1 line
anyone can help me?

sample like this :

Dim OutputPath As String
Dim sBaris$
Dim DROP() As Byte
Dim strTeks As String

OutputPath = "C:\Windows\Temp\RPLRECORD.TMP"

DROP = LoadResData(RPLRECORD.TXT, "CUSTOM")
Open OutputPath For Input As #1
Line Input #1, sBaris$
strTeks = strTeks & sBaris$
Close #1

Me.Text1.Text = strTeks

Thanks

Dani AI

Generated

Short summary and next steps (for ): reading only a single “line” usually means one of three things — the .tmp is not plain text (binary or different encoding), the file actually contains no newline characters (one long line), or you’re reading before the writer has finished/closed the file. ’s line-by-line loop fixes plain-text files; below are alternative diagnostics and fixes that cover encoded or binary .tmp files and let you grab the whole file in one go.

A quick whole-file read (FileSystemObject)

  • Use the FileSystemObject to pull the file in one call; this returns the file as a string when it’s plain ANSI text.
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim ts As Object
Set ts = fso.OpenTextFile(OutputPath, 1) ' ForReading
Text1.Text = ts.ReadAll
ts.Close
Set ts = Nothing
Set fso = Nothing

Handling UTF-8 / UTF-16 or unknown encodings (ADODB.Stream)

  • ADODB.Stream can load the file as binary then convert using Charset. Try "utf-8", "unicode" (UTF-16 LE) or the system codepage if ReadAll gives garbage.
Dim stm As Object
Set stm = CreateObject("ADODB.Stream")
stm.Type = 1          ' binary
stm.Open
stm.LoadFromFile OutputPath
stm.Position = 0
stm.Type = 2          ' text
stm.Charset = "utf-8" ' try "unicode" or other values if needed
Text1.Text = stm.ReadText
stm.Close
Set stm = Nothing

If you suspect a BOM or binary file, read the first bytes to detect encoding (hex EF BB BF = UTF‑8 BOM, FF FE = UTF‑16 LE, etc.) and use the appropriate conversion or ADODB.Stream. For purely binary data you’ll need to interpret the bytes, not use line-based reads.

A couple of practical notes: LoadResData is for resources compiled into the EXE (not for reading arbitrary disk files). If another process creates the .tmp, make sure it has finished writing and closed the file before you read it; otherwise you may see truncated or partial data. If the file is very large, avoid loading everything into a single TextBox — read in chunks or show the file in a viewer. If you post the first few hex bytes here I can point to the exact Charset or conversion to use.

Recommended Answers

All 4 Replies

Make sure the Text Box MultiLine property is set to True
You may have to add a vbcrlf to the text between reading lines.

Well, there is a bit more to it than that. Sorry I did not test before posting.
The following code works for me.

Private Sub Form_Click()
Dim FSO As New FileSystemObject
Dim OutputPath As String
Dim sBaris$
Dim DROP() As Byte
Dim strTeks As String

OutputPath = "C:\Program Files\Blank Calendar\MyBD.txt"

'DROP = LoadResData(RPLRECORD.TXT, "CUSTOM")
Open OutputPath For Input As #1


If Not EOF(1) Then
    Line Input #1, sBaris$
Else
    MsgBox "File Empty"
    Exit Sub
End Sub

Text1.Text = sBaris$

Do While Not EOF(1)
    Line Input #1, sbarist$
    Text1.Text = Text1.Text & vbCrLf & sbarist$
Loop
Close #1

'Me.Text1.Text = strTeks
End Sub

thank you for reply sir, but file which i want to access is on temporary file (.tmp). from my script on the top, i just can access it in the line 1 not more.

Well, that isn't what you ask. You asked about .txt files and your example uses .txt file. The "Line Input #" method is for use with sequential (text) files.

There are two issues here.
First, Did the code I posted work for the code you posted? If that code works would you please mark this thread as solved. I don't see how the code you posted would produce anything more than one line in the text box. You will have to loop the .txt file to capture all lines, and your code does not do that.

Secondly, If you have a question about reading .tmp files I suggest you post a new question that states that question and indicates code that attempts that.

For what it is worth:
From what I can find on temp files: They can be anything that an Internet program might want to save to your computer. They might be text, but might not, and what you get may only be gibberish especially if trying to open an exe.
It appears that most files can be opened in NotePad (select "All" under file type). Opening the file in NotePad may provide you with what your code was trying to do.
I delete all tmp files, so I have none to play with.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.