0

The following VB 6 code saves text from the textBox in the form GUI to file.txt (By the click on the button)

How to do the reverse option – copy/capture file text (file.txt) , and passed it on the textBox in the form GUI , I will happy to get real example

remark - (before passed need to clear the form window from any text )

  Private Sub save_Click()

    saves = (Form1.Caption)

    FCO.CreateTextFile App.Path & "\" & saveas & "file.txt", True

    FCO.OpenTextFile(App.Path & "\" & saveas & "file.txt", ForWriting).Write Text1.Text

  End Sub
  • possible duplicate of [How can I read data from a text file using VB6?](http://stackoverflow.com/questions/2873830/how-can-i-read-data-from-a-text-file-using-vb6) – MarkJ Jan 05 '12 at 12:36

2 Answers2

0

You can load in the contents of a file using the standard VB6 file I/O operations

Dim FileNum As Integer
Dim Size As Long
Dim Data() As Byte

'Open the file
FileNum = FreeFile()
Open FileName For Binary As #FileNum

'Read all the data
Size = LOF(FileNum)
ReDim Data(Size - 1)
Get #FileNum, , Data
Close #FileNum

'Convert to a string
TextBox.Text = StrConv(Data, vbUnicode)

See the original article.

Deanna
  • 23,876
  • 7
  • 71
  • 156
  • Shorter to use the method from the VB6 manual. As in my [answer](http://stackoverflow.com/a/2875248/15639) to this duplicate question – MarkJ Jan 05 '12 at 12:37
0

This reads from a file and puts the results in Text1.Text.

Private Sub save_Click()  
    Dim sFile as String
    sFile = "c:\whatever"
    Dim sData as String
    Dim fnum as Integer
    fnum = FreeFile()
    Open sFile For Input As #fnum
    If Not eof(fnum) Then
         Input #fnum, sData
         Text1.Text= sData
    End If
    Close #fnum
End Sub
jmoreno
  • 12,752
  • 4
  • 60
  • 91
  • I get compliation error on line --> If Not eof(fnum) , do you know what could be the problem ? –  Jan 04 '12 at 20:02
  • @Eytan: yeah, I left off the Then. Sorry about that – jmoreno Jan 04 '12 at 20:20
  • another problem - file content not displayed in the text box in the form GUI ? , I mean file text not appears in the text box after click on the bottom its appears on the form title - I update my question about the textBox –  Jan 04 '12 at 20:38
  • @Eytan: This reads a single line of text, I'll leave it to you to handle if you want more than one line (hint: use a do while loop). – jmoreno Jan 04 '12 at 20:44
  • @Eytan, Your code sample also uses the form caption. Do you *really* need help assigning a string to a Textbox? To be honest, I don't even answer questions that basic. – tcarvin Jan 04 '12 at 20:45
  • Ok thanks allot about your great answer you help me allot, I was start to learn VB6 from this week –  Jan 04 '12 at 20:54