Another essential part of Visual Basic is file Input/Output, that is, working with files. While programming, you may want at some point to save data so they may be accessible for further use. This is where file I/O comes in. VB allows us to perform most operations available in Windows Explorer and DOS command line. Before you can do this, you need to understand how file I/O works.

Layout of Data edit

VB generally arranges data into records and fields for the programmer's convenience. The best way to explain this is by illustrating it:


This is obviously data containing names. Each line of data is called a record. So the first record contains Bob, Larry, and George. A field is each item in a record. So the first field in the first record would be Bob, the second field would be Larry, and so on. There may be as many fields and records as you see fit.
Records and fields are created really just for the programmer's convenience. For greater flexibility, you can also view the whole thing as a string. And you can split the string into records and fields yourself with the built-in Split() function.

Input edit

When a program receives data from an outside source, it is considered input. In Visual Basic, this source is usually a standard text file, with a .txt file extension(as which could be read with Notepad). First, you need to open the file from which you want to read the data. This is done using the following code:

 Open <filename> For <mode> As <channelnumber>

For example:

 Open "c:\filename.txt" For Input As #1

The file path can be anything you want, if it doesn't exist, a new file (and directory(s)) will be created. The extension of the file doesn't matter much. It will not affect the content of the file nor the process of writing/reading the file. So you can also do this:

 Open "c:\filename.myfile" For Input As #1

Or this:

 Open "c:\filename" For Input As #1

The open and file path are self explanatory. However, "for input" states it is being used to receive data from an outside file, VB is "inputting" data from a file into variables . When outputting(writing to a file), this is changed to "for output". When we want to open a file in binary, it's "for binary". "As #1" is which channel is being used. When you are seeking specific information from the file you would use "for random". A channel could be seen as Visual Basic setting up a pathway to the file. Only one file can be used on a channel at a time. So if you want to open multiple files without closing any, you can use different channels. To input a line of data, it is fairly simply:

 Input <channel>, <variables>

For example:

 Input #1, x, y, z

This will read data from whatever channel 1 is connected to. It will store the first field of the current record into X, and the second field into Y, and the third into Z.
Finally, we should ALWAYS close the file after the work is done. The syntax is simple:

 Close <channel>

As an example:

 Close #1

More on using input will be covered in the "Use of Data" section.

Note: If the code will be part of a larger program, the channel number should be obtained in a dynamic way instead of a hard-coded number. This is done using the function FreeFile()

  Dim nLogFile As Long
  nLogFile = FreeFile()
  Open "c:\filename" For Input As #nLogFile
  Close #nLogFile

Output edit

Output is very similar to input. The only difference is, well, it sends information OUT of the program and into a file. Once again, we shall only use text files (.txt). First you must open the file:

 Open "C:\filepath.txt" For Output As #1

You may have noticed, the only thing that differs from opening a file for input, is the "For Output". Once this is done, everything else is simple.

 Write #1, x, 5

Assuming X = 3, if you open the text document it will look like:

 3, 5

You can write any value or variable to a file, however it is important to note that, when writing a string, it should be placed in quotes. If it is a variable containing a string, this is done automatically. Another important note is that two consecutive write statements will write to separate lines. For example:

 Write #1, 5, 4
 Write #1, 7, 4
 Write #1, "this is a string"

Will appear in the file as

 5, 4
 7, 4
 "this is a string"

Keep in mind, when outputting to a file, it will ERASE all prior information in the text. To add on to existed information, the "Append" command must be used.

If you are writing data to a file for use by some other program, you may find that you don't want the quotes that VB puts around your strings. You can avoid writing the quotes by using the Print command instead of the Write command. For example:

 Print #1, "this is a string"

Will appear in the file as

 this is a string

If, during runtime, the file at hand was modified, and a new line was added which contained necessary data, and the open statement ended as "#1", whatever was in line 1 of the file would be written over(only if the file was opened as Output). To solve this, a variable is dimmed as FreeFile (Dim FF as FreeFile). This will open the first free slot or line in the file at hand. The new Open statement would become:

 Open "C:\filepath.txt" For Output As FF

(will erase file)

 Open "C:\filepath.txt" For Append As FF

(will not erase file)
Using the second statement, the first free line in the file will be written to.

How to specify the path to a file edit

In practice it is rare for open statements to look like this:

 Open "C:\filepath.txt" For Append As FF

Usually the path to the file will be held in a string variable:

 Dim s As String
 s = "C:\filepath.txt" 
 Open s For Append As FF

The variable, s, simply needs to hold a path to the file. The path can be an absolute path such as:

 s = "d:\users\me\my file.txt"

or a file in the current directory (use CurDir to find out what that is):

 s = "my file.txt"

or a path relative to the current directory like this:

 s = "..\you\your file.txt"

or like this:

 s = "mysub\myother file.txt"

For the more adventurous it can also include a reference to a shared file on a server that doesn't use a drive letter:

 s = "\\someserver\someshare\somefile.txt"

Examples edit

An example form using file reading can be found here. Right click 'here' in Internet Explorer and then 'Save Target As...' to save the form file.

Please upload more files with examples. It will be really helpful for the developer.

Previous: Arrays Contents Next: Data Types