Add a Save and Load Function to your application


Declaration

‘Add a button captioned “Save” with the name Command1
‘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

‘code

‘—————————
‘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

chrismanquiz@yahoo.com

author: Chris Anselmo

2 responses to “Add a Save and Load Function to your application

  1. Pingback: Trackback

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s