by Chris Anselmo (1 Submission)
Category: Files/File Controls/Input/Output
Compatability: Visual Basic 4.0 (32-bit)
Difficulty: Unknown Difficulty
Originally Published: Mon 11th August 2008
Date Added: Mon 8th February 2021
Rating: (1 Votes)
Add a "Save" and "Load" function to your application
API Declarations
'Add a button captioned "Open" with the name Command2
'Add 2 text boxes with the default names (Text1 and Text2) in any order
'Make sure the "Save" button's name is Command1 and the "Open" button's name is Command2
'The "Save" Button:
Private Sub Command1_Click()
Open "C:\test1.txt" For Output As #1 'Open a test file
Print #1, Text1.Text 'Write text1's text in the first line of the file
Print #1, Text2.Text 'Write text2's text in the second line of the file
Close #1 'Close the File
End Sub
'-------------------
'The "Open" buton:
Private Sub Command2_Click()
Open "C:\test1.txt" For Input As #2 'Open's the same file
Dim line1 As String 'These variables are needed to read each line correctly
Dim line2 As String 'This is for the second line
Input #2, line1 'This writes the fist line of the file to the variable line1
Input #2, line2 'Same as above, except the second line for the variable line 2
Text1.Text = line1 'This sets the text boxes text to the corresponding variables
Text2.Text = line2
Close #2 'Close the file
End Sub
'--------------------
'Make sure when you test the program, that you press the "Save" button first.
'I haven't put in error handlers so the program won't see the file if you
'click the open button first
'-------------------------
'If you need help or you want to use this technique in a more advanced way,
'E-mail me