Previous page Previous     TOC Contents     Next pageNext


LESSON 8 - Working with files




Storing data

Data comes in many forms. It can be a list of DVDs you own and want to keep track of, the description of all the online college courses you intend to take or even the movie stars you intend to date!





In the previous lessons, you have learned how to manipulate the VB environment to produce forms, do calculations, edit text and so on. However, everything you've done so far is a one-shot deal. Even if you did create the Payroll form, you can use it to calculate the net pay for any number of employees but, you can't save any of that information.

That's where data storage comes in. There are many ways to store data for future use. The most popular and powerful method is to create a database. But that can get quite involved and it does require a certain amount of analysis knowledge and skill. The next two lessons will cover how to create and use databases in VB.

A much more accessible method and one which you have certainly used many times before, is to create a data file. A file is a collection of data on a given subject, stored on a storage medium, usually a disk or CD. There are executable files, usually with the .EXE extension, library files (.DLL), Word document files (.DOC) and a hundred other types. Many applications call for data to be stored and then read back later for further processing. Think of a simple application: an Address book to store people's names, addresses and phone numbers. You could create an Address book database and indeed, it is often the first one you learn how to do in database courses. However, the task is more suited to data file processing. You just want to create a form to input names, addresses and phone numbers and then you want to store all the information entered in a file so that you can print it or look-up numbers when needed. In this lesson we will learn how to create our own files to store and retrieve data.


Defining new terms

  • Record: one logical section of a file that holds a related set of data. If the file contains Student information, a record would hold the information on one student: name, address, studentID, etc. If there are 5,000 students registered, the file contains 5,000 records.

  • Field: part of a record that defines a specific information. In the Student record, FirstName, LastName, StudentID, are fields. The field is the lowest element in the file. Even if the information consists of one character, Sex is M or F, it is still considered a separate field. The field is the equivalent of the variable - we call it a variable when it is used to store data in memory and call it a field when it stores in a file.

  • I/O: stands for Input/Output. Whenever you work with a file you have to have ways of reading data from the file (that's Input) and ways of writing data to the file (that's Output). I/O operations consist of all those commands that let you read and write files.



Types of files

There are basically three types of files you can work with:

  • Sequential file: this is a file where all the information is written in order from the beginning to the end. To access a given record you have to read all the records stored before it. It is in fact like listening to a tape - you can go forward or back but you can't jump directly to a specific song on the tape. In fact, in the old days, magnetic tape was the most commonly used medium to store data and all files were organized this way. Now, it is still useful when there is a small amount of data to store, a file of application settings, for example. It can even be of use when there is a large amount of data to be stored, provided it all has to be processed at one time, eg: a file of invoices to produce a statement at month-end.

  • Random file: a file where all records are accessible individually. It is like a CD where you can jump to any track. This is useful when there is a large quantity of data to store and it has to be available quickly: you have to know if a part is in stock for a customer who is on the phone; the program doesn't have time to search through 10,000 records individually to locate the correct one. This method of storage became popular when hard-disk drives were developed.

  • Binary file: this is a special, compacted form of the random file. Data is stored at the byte level and you can read and write individual bytes to the file. This makes the file access very fast and efficient. We won't be covering this type of file in these exercises. If you need to find out more about it, go to the VB Reference Manual.

Opening and closing files

To begin our work on files we will look at some commands that are common to both Sequential and Random files. After that we will look at the specific processing commands for each type of file.

The first command to include in a program that needs to work with files is the Open command. Open assigns the file to a numbered file handle, also called a channel, or sometimes a buffer. The format of the command is:

Open "Filename" [For Mode] [AccessRestriction] [LockType] As #FileNumber


For example:
Open "MyFile.txt" For Random Read Lock Read As #1
  • MyFile.txt is the name of the file in the disk directory.
  • For Random means that access to the records can be random; if access is not specified, For random is the default value.
  • Read restricts access to Read-only - the user cannot write or change the records.
  • Lock Read means that only the person reading the record can have access to it at any given time; it is not shared among users.
  • As #1 means the file is assigned file handle #1; for all processing in the program, it will always be refered to as #1, not its Filename.


AccessRestriction and LockType are parameters that are used mostly with files in a network environment. You use them when you want the file to be shared or not, and you want to prevent certain users from changing or deleting things that they shouldn't. For the rest of this lesson we will not be using those parameters.

Access Mode

For Mode in the Open statement indicates how the file will be used. There are five access modes:
  • Input: open for sequential input; the file will be read sequentially starting at the beginning.

  • Output: open for sequential output; records will be written sequentially starting at the beginning; if the file does not exist, it is created; if it does exist, it is overwritten.

  • Random: open for random read and write; any specific record can be accessed.

  • Append: sequential output to the end of an existing file; if the file does not exist it is created; it does not overwrite the file.

  • Binary: open for binary read and write; access is at byte level.
If access mode is not specified in the Open statement, For Random is used by default.


Once processing is finished, you need to Close all the files that have been opened. The format for the Close statement is:

Close #FileNumber1 [, #FileNumber2] ...

You can close any number of files with one Close statement. Eg:

Close #1, #2, #3

The following statement closes all open files:

Close








Writing and Reading a Sequential file

There are two commands that allow you to write data to a sequential file: Print and Write. They work in almost the same way but, the Print command does not separate the fields in the file in quite the same way which makes the data harder to read afterwards. There is really no valid reason to use Print when creating a sequential file. In the rest of this lesson we will use Write exclusively.

The format of the Write command is:

Write #FileNumber, OutputList


where FileNumber is the number the file was opened with and OutputList is one or more variables you want to write to the file.





Address Book Example

In this exercise we will create a simple address book file to keep track of people's names, addresses and phone numbers.

To handle the various forms that we have to use, we will develop a new technique for these lessons: the use of a Menu of choices. Note that that is not the same as a Menu bar used in a form. In this case we are just going to line-up a series of buttons for the different forms that have to be called. There has also been a small change to the display format - from now on all the forms are maximized (they occupy the full screen) - this is often easier for the user to work with, rather than have a number of different forms overlapping on the screen. To get the form to run maximized, change the Form property WindowState -> 2 - Maximized.

This is what the menu should look like:



The code for the menu consists of loading and showing the various forms. The Exit button exits the Menu itself. Any open files are closed by the individual forms.



File design

It has been determined that the file will store 7 fields of information. First and last names could be together and we could have a work phone number but, the Analyst (who gets paid big bucks to think this stuff up) has determined that 7 is what is required. It has also been decided that the file will be called "AdrsBook.txt" and will be stored in "C:\VBApps" - we need to know this for the Open statement.

It must also be determined, before we start to code, what the File mode is going to be when we output to the file. We could use "Output" but that would mean that every time that we want to add a new listing, we wipe-out the file. Not very practical! Therefore, we will use "Append" so that all new entries are added to the end of the existing file.

Finally, once the controls are in place on the form, we have to finalize the order in which we Tab through them when working from the keyboard. That is called the Tab order. To set the tab order, we use the TabIndex property for each control. It starts at 0 and goes up for every control in order. When the form opens, the control with TabIndex=0 gets focus; when you tab from that, focus goes to TabIndex=1, and so on. Controls that don't get focus - Labels, Pictures, etc. - do have a TabIndex but their TabStop property is set to False. If you don't want Tab to stop on a control, set its TabStop to False.

Here is what the Sequential Output form will look like when we use it:



Once the file has been created we can use Notepad to look at it. Notice that the last entry, the one on the form above, is not yet in the file. It gets written only when you hit the Write button. Each field entered is stored as a separate line in the file. When we read them, we read in the same order as that in which they were written.



Creating the Sequential Output form

The form SAdresOut is used to capture data from the user and then output that data to the AdrsBook.txt file. The design of the form is what you see in the diagram above.

As you can see, we need 7 TextBox controls to capture the 7 fields. To simplify the code, we will use a technique we haven't used before in these lessons: the Control Array. You may have seen that come up before if you tried to copy and paste controls. What we do is: create one TextBox control, give it a name - we call it "txt_field" -, and then copy that control and paste it 6 times on the form. When you paste a control, since it has the same name as the existing one, the editor asks whether you want to give it a new name or create a control array. In this case we tell it to create the control array. This means that, instead of 7 different TextBoxes, we will have an array of TextBoxes, named txt_field(0) to txt_field(6). As you can see from the code, this allows us to use For ... Next loops to do things like clear the controls and write to the file.

The Cancel button simply clears all the TextBoxes and does not executes a Write operation. The Exit button closes the open files and unloads the form which returns us automatically to the Menu form. There is no End statement, as that would cause the program to end.



The code to write to the file is fairly straightforward. Once information has been entered into the 7 TextBoxes, we use a FOR ... NEXT loop to execute the Write command. The reason for this is that the Write command outputs only one field at a time. So, we have to do 7 writes to output the whole record. After the TextBoxes have been written-out, we clear them to create the next record.









Working with a Random file

For this exercise we will use the same Menu form that we started with but we'll create a new output file which we will call "PhoneBook.txt". Since this file format is different from the sequential, we can't use the same file to test the code. The PhoneBook file will have almost the same content as the AdresBook file. The only difference is that we'll add a field for PersonId at the beginning of each record. That will allow us to retrieve records using a record number.

User-defined data type

In addition to data types like String, Integer, Date and so on, you can also define your own data type. This type is called structure or structs in other languages. We will use it in our application to simplify our I/O operations since our I/O commands, Put and Get only handle one field at a time. What we do with the user-defined data type is to create a new variable which contains a whole record.

The user-defined variable must be declared in a module. That's a program at the application level, not tied to any specific event. To create a module: Menu bar --> Project --> Add module --> Open. When you save the module, it will take the .BAS extension. The information contained in modules is available to all the forms in the application. This is what your first module should contain:



The Type statement creates a new data type; in this case, it's PhoneRec. Once it's been defined, the new type can be used like any other type, String or Integer, etc. to declare a variable:

Dim InRecord As PhoneRec


The individual fields in the structured variable can be accessed using dot notation:

Label5.Caption = InRecord.Fname
txt_city.Text = InRecord.City


When you define the fields within the new type, it's important to determine the length of each string. Random access is sensitive about record lengths. When you define a String field like: Fname As String * 15 you determine that the size of the field will always be 15 characters. This is important for the processing to work properly! Just make sure that the size you assign is big enough to handle most situations. You do not have to worry about the Integer field because its size is standard.

Writing and Reading records

The command to write records to the Random file is Put. Its format is:

Put #Filenumber, [RecordNumber], Variable

RecordNumber is optional and, if it's omitted, variable is written in Next record position after last Put or Get statement.

The command to read records from a Random file is: Get. Its format is:

Get #FileNumber, [RecordNumber], Variable

If RecordNumber is omitted, next record is read from the file.

Creating the Random file

To create the PhoneBook file, we will need a new form which is just a copy of the SAdresOut form with the additional Person number TextBox, which is in fact the record number. Then we'll write the code, making use of the user-defined data type "PhoneRec" described earlier. This form, "RAdresOut", obtains the next record number from the file, accepts input from the user and writes- the record out to the file.







To read records from the file, we have to specify a record number. This number is accepted into the Person number TextBox and then used to locate the appropriate record in the file.

The error-trapping routine is useful in this procedure because you are almost certain to encounter the "Reading past End-of-file" error when you enter a Person number that does not exist.








You might also want to visit this site for
Free Visual Basic 6 tutorials and sample source code examples .









If you haven't found the Visual Basic resource you're looking for,
use our Google Search box for more information.

Google