Visual Basic/vb6 save load text file
Expert: Golfnut_1969 - 11/22/2007
QuestionQUESTION: hi and thanks for helping
i have 3 text boxes and a command button
when i click the command button i want the text inside the 3 text boxes to save inside a text file
iam able to do this but I need to repeat this couple of times so
every time I click command button it saves a new line
text1 text2 text3
text1 text2 text3
now I want to display this inside a listbox and also a button that will load this text back to the 3 text boxes
hope iam clear and i hope this isnt to much to do
Thanks allot i appreciate it very much
u.soylu
ANSWER: First of all, I suggest you reference the Microsoft Scripting Runtime library (scrrun.dll). This will make your life easier when reading and writing to files. It's simple to use and is going to resemble .NET's methods of file access.
Here is an example that does what you need. My command button is named cmdWrite and my textboxes are named txtValue1, txtValue2 and txtValue3.
Private Sub cmdWrite_Click()
Dim sFile As String
Dim oFSO As FileSystemObject
Dim oTS As TextStream
sFile = App.Path & "\MyFile.txt"
Set oFSO = New FileSystemObject
Set oTS = oFSO.OpenTextFile(sFile, ForAppending, True)
oTS.WriteLine (txtValue1.Text & " " & txtValue2.Text & " " & txtValue3.Text)
oTS.Close
End Sub
There are a couple of things you need to consider. One is that there is is nothing that delimits the text. This will be a problem when you want to read it out of your file as you won't know where the text from txtValue1 ends and the text from txtValue2 begins once its in the file. In my example, I've delimited each text value with a space but this will cause a problem if any of the textboxes have a value containing a space. To work around this we will delimit the values with a unique character we know the user won't be typing. I like to use Chr(255). So, we need to modify the code that writes the line to look like this.
oTS.WriteLine (txtValue1.Text & Chr(255) & txtValue2.Text & Chr(255) & txtValue3.Text)
Now we have the values in a text file so we need to add code to get the data back out of the file. For that we will use the File System Object and Text Stream again. We will open our file and read each line, loading the line into the Listbox each time. Also, I'll add code to our cmdWrite button's click event to add the line to our Listbox as well. In this way our Listbox will always reflect what is in our text file. Since our cmdWrite button will add new items to our listbox, we only need to load our Listbox from the file when the form first opens. We can do this in the Form_Load Event.
I've added a Private Property to the form that will give us the path and file name of our text file. Here is all the code you will need. So, if you add a new form to your project, add the following controls and copy and paste the code below, you should have a working application.
txtValue1 TEXTBOX
txtValue2 TEXTBOX
txtValue3 TEXTBOX
cmdWrite COMMANDBUTTON
lstItems LISTBOX
Option Explicit
Private Property Get MyFileName() As String
MyFileName = App.Path & "\MyFile.txt"
End Property
Private Sub Form_Load()
Dim oFSO As FileSystemObject
Dim oTS As TextStream
Dim sLine As String
Set oFSO = New FileSystemObject
If oFSO.FileExists(MyFileName) Then
Set oTS = oFSO.OpenTextFile(MyFileName, ForReading)
Do While Not oTS.AtEndOfStream
sLine = oTS.ReadLine
lstItems.AddItem (Replace$(sLine, Chr(255), vbTab))
Loop
oTS.Close
End If
End Sub
Private Sub cmdWrite_Click()
Dim oFSO As FileSystemObject
Dim oTS As TextStream
Dim sLine As String
Set oFSO = New FileSystemObject
Set oTS = oFSO.OpenTextFile(MyFileName, ForAppending, True)
sLine = txtValue1.Text & Chr(255) & txtValue2.Text & Chr(255) & txtValue3.Text
oTS.WriteLine (sLine)
lstItems.AddItem (Replace$(sLine, Chr(255), vbTab))
oTS.Close
End Sub
---------- FOLLOW-UP ----------
QUESTION: THANKYOUU YOUR THE BEST OF THE BEST THIS HELPS ALOT
but one more thing please
lets say I have another form called form2 and I have a listbox2 and a commandbutton2
the listbox2 needs to show the text file I have saved in the first place and when commandbutton2 is pressed the selected line in the listbox2 will appear in form1 inside the 3 text boxes
--
i appreciate it Very Much
--
AnswerI read your follow up but I'm not sure what you are trying to accomplish. Why do you need a second form? Why not use the listbox in Form 1 to accomplish this? When you click on the listbox item, simply set the Text values of the textboxes to the items in the row of the listbox you have clicked on.