Lesson XVIII:: Sequential File Handling
Lesson XVIII:: Sequential File Handling
Objectives
To be able to open and close sequential files using Open and Close
To be able to detect end-of-file condition
To be able to write to files using Write
To be able to fetch data from files using Input
Notes
Visual Basic offers file handling capabilities and database handling features. In database handling
controls, the file manipulation is transparent to the programmer and user. However, in nondatabase applications the programmer has to handle virtually all aspects of reading or editing the
data contained in a file. This is what this lesson will cover.
Simple text files (also called sequential files) are often used as the storage method for information.
This is because of their universally standard format. You can simple create text files using DOS
EditTM program and the newer Windows NotePadTM applications.
To start manipulating files, you must first open it. You do this through the following:
Open <filename> for <mode> As # <filenumber>
Close #<filenumber>
Open tells Visual Basic to locate a file specified by <filename>, open it for some purpose, and in
some cases, create the file if it does not exist. <filename> can be a string literal such as
D:\myfiles\names.txt or a string variable containing the name of the file to be opened. <mode>
tells Visual Basic what to do with the opened file. It can be any of the following:
Mode
Input
Output
Append
Random
Description
Opens files for reading. An error occurs if the specified file
does not exist.
Opens files for writing. If the specified file does not exist, VB
creates the file for writing. Note: It overwrites the contents
of the file specified.
Opens files for appending. It appends new data to the
contents (if there are any) of a file. If the specified file does
not exist, VB creates the file.
Opens files for reading or writing.
The pound sign (#) is optional. The <filenumber> value (also called file channel), which can be
between 1 and 255 inclusive, represents the specified file. If you need to manipulate a file, you
refer to the file by the <filenumber> to which it is associated. The number remains associated to
the file until such the time when you close the file using Close statement. Always remember that a
<filenumber> can only be associated to one file only at one time. If the file is closed, you may use
the <filenumber> again for some other file. If you have lost track of the available <filenumber>,
you may use the FreeFile() function as in the following statement:
FileNum = FreeFile()
The Close statement on the other hand performs the opposite job from Open. It closes a file and
releases the <filenumber> for reuse by the Open statement. There are 2 ways in which you can
use this statement. They are as follows:
Close #<filenumber>, , #<filenumber>
e.g.
Close #1
Close
The first Close format closes one or more files as specified by the <filenumber>.
format close all opened files, thereby releasing all <filenumber>s.
The second
Close #1
This code works but with one flaw: Every time we iterate the Input statement, we overwrite the date
previously stored in StdName and StdAge. This does not serve our purpose, after all! What we
should do then is to store the contents in an array.
Open C:\studerec.txt For Input As #1
x=0
Do Until (EOF(#1) = True)
Input #1 StdName(x), StdAge(x)
x=x+1
Loop
Close #1
After 3 iterations, we will have the following data:
StdName(0)
StdAge(0)
Antonio
24
StdName(1)
StdAge(1)
Heidi
25
StdName(2)
StdAge(2)
Joni
29
Lesson in Action
Lets create a simple file handling application. The Form below contains a TextBox, a ListBox and
2 CommandButtons. The Add Name button adds the name entered in the TextBox to the
ListBox. Save to File button saves the contents of the ListBox in a text file for future use.
On your Own
Instructions: Modify the sample program to include not just the name but also the age of the
person. The program should also be able to delete a person and edit a persons data.
The < and > buttons are for browsing through the student records. When > is pressed, the
program displays the records of the next student (if any). Disable this button when there are no
more succeeding records. When < is pressed, the program displays the records of the previous
student (if any). Disable this button when there are no more preceding records. The records of
the first student (the one stored at index 0 of the array), if any, should be displayed during startup. The TextBoxes containing the students name and age should be locked to avoid being
edited. Unlock these TextBoxes only when the user adds a new student (when the New button is
pressed) or when the Edit button is pressed. Disable Edit and Delete buttons when there are no
displayed records. The Save button should be disabled until the user adds a new student
record. Use two arrays StudeName() and StudeAge() to store the students data and store these
in StudeRec.txt.