Saving Files

Visual Basic has built in file handling functions. These are not the fastest functions but it is not noticeable unless you are handling very large files. There are two common ways which we will cover in this tutorial. I consider these two functions to be the simplest.

Opening the file

Before we can save anything into the file we have to prepare things a bit. You could say that we are opening a connection with the file but this is not technically accurate. If the file does not exist then VB will create it for you at this stage. The syntax is this:
Open "C:\folder\file.txt" for Output As #1

Close #1
A good idea for the path of the file is to use App.path so that the file will be saved in the same folder as the program. Obviously you must have saved the project for this to work. If the program is compiled then the executables path is used.

Another way to get a nice path is to use a common dialog save box if the user is choosing the path.

The For output tells VB what you will be doing with the file. It is for Output because we are going to output data from the program. There are several different things that could go here ie.Input. There is actually a general one but we won't use it for efficiencies sake.

As #1 gives a reference number which we can use when dealing with the file. Every different file you open must have a unique number otherwise VB will generate an error. You can have an integer variable instead of typing the number here.

You should always close the file when you are finished. After the file is closed its number is availiable for use. I believe that VB will close the file when you program ends but its always best to tidy up yourself.

The Write function

This is the first method for saving a file, It works well and unless the file is not your own format and not to be read only by your program this should be suitable. Exceptions that I have come across are when I was making a html editor and opening someone else's file format for a game.

Here is a small example program which should clearly demonstrate how to use the output function and what kind of output it gives. It will put the file in the same directory/folder as your program under the name "writefile.txt". Just create a new project with a command button in it and remember to save the project. Here is the code for the project.
Private Sub Command1_Click()
Open App.Path & "\writefile.txt" For Output As #1
    Dim a As String, b As String, c As String, d As Integer
    a = "hello"
    b = "world"
    c = "bob says hello"
    d = 5
    Write #1, a; b; "(and some non variable text)"
    Write #1, c
    Write #1, d
Close #1
End Sub
The main thing to notice is the semi-colons seperating the different srtings. You can replace these with commas to get a very similar effect (you will get a space after the comma). Now open the file that has just been created (its in the directory where you saved your project). You should be able to see how this funtion works.
"hello","world","(and some non variable text)"
"bob says hello"
5
If your file does not look like this check your code carefully.

You will notice that strings are in quotes but numbers aren't, just like when you have to declare variables. Also you should see that the variables fro the same write function ar on the same line and are just seperated by commas. Finally each new write statement is on its own line. The benefit of having all this is that you can just read back the variables in exactly the same way with no worries about where one starts and the other ends.

The Print function

This is identical in the way you use it and the output is very similar.

This is really where you shoudl use the semi-colons instead of commas as commas cause large spaces to be put between strings.

You will see that the speech marks are gone and so are the comma's.

© Jonathan Waller 2005; QuantumState Visual Basic