On the previous pages, you learned how to use the list box object, and how to use sequential access files. On this page, you will learn how to read from sequential access files. Just so you know ahead of time, reading from this type of file is no big deal! I myself cannot think of a good reason to do it or use it. We could accomplish the same task using arrays, which you will learn about later in this tutorial. Sequential access files need to be read sequentially as well (down the list of records one at a time), so we cannot skip around through the file.
Set up the interface below:
Set the form's Width property to 4770 and the form's Height property to 2940.
Name the form frmTelephone
Name the list box lstNames.
Name the label above the list box lblName.
Name the other label lblPhone.
Place a label beneath that label and line the left edges up.
Name that label lblNumber.
Be sure to set the AutoSize properties of all the labels to True.
Name the command button cmdExit.
Code cmdExit to end the application.
Lock the controls on the form.
Set the TabIndex property of the objects as follows:
lblName: 0
lstNames: 1
cmdExit: 2
Be sure to set the access keys appropriately as seen in the image above.
Set the form's BorderStyle property to 1-Fixed Single and the MinButton property to True.
Set the form's StartUpPosition property to 2-CenterScreen.
Now you are going to do something a bit differently.
Open a simple text editor, either Notepad or Wordpad, and type the following lines:
"Joe Shmoe"
"Jennifer Green"
"Alex Price"
"Jeff Skinner"
"Ashley Ryder"
"Susan Williams"
Save the file as Names.dat on the desktop.
Now we will code this application. We are going to use three form-level variables in this program; bytSelection to store the index of the selected item in the list box; strPhone to store the appropriate phone number of the name in the list box; and strName to store the entry read from the sequential access file.
In the General Declarations section of the code window, type the following lines:
Dim bytSelection As Byte
Dim strName As String, strPhone As String
We simply declared three form-level variables; two as strings and one as a byte.
Open the code window for the list box's click event procedure, press tab, then type the following lines:
bytSelection = lstNames.ListIndex
Select Case bytSelection
Case 0
strPhone = "775 352-0954"
Case 1
strPhone = "775 352-3425"
Case 2
strPhone = "775 352-3792"
Case 3
strPhone = "775 352-5464"
Case 4
strPhone = "775 352-8236"
Case 5
strPhone = "775 352-4476"
End Select
lblNumber.Caption = strPhone
This code simply sets the variable bytSelection to the ListIndex property of the list box, then looks at each entry using Select Case. It stores a number in the string variable strPhone, then displays the result in the label lblNumber.
Well at the moment, we do not have any entries in the list box, so what are we going to add to it? Simple; the names we entered into the data file in the text editor. We will add the names when the form loads.
In the code window for the form's Load procedure, press tab, then type the following lines:
Open "C:\Windows\Desktop\Names.dat" For Input As #1
Do While Not EOF(1)
Input #1, strName
lstNames.AddItem strName
Loop
Close #1
Line 1: Open the data file Names.dat, which we
created on the desktop for Input (read--get data) as file number one.
Line 2: Begin Do While loop; do statements below while the
computer has not read to the end of file (EOF) number one. We need
to use a loop because the computer otherwise reads only one record from the data
file. It does this just because that is the way the computer reads sequential
data files. The loop keeps on reading the file, therefore it keeps on getting
the records.
Line 3: Read file number one (Input reads the file), and store the record
in the file as strName.
Line 4: Add the record stored in strName to the list box lstNames.
Line 5: Integrate the loop; keep reading in order to get all the records.
Line 6: Close the file.
Below the last line that you just entered, type lstNames.ListIndex = 0.
This line simply selects the first item in the list box. The application should work just fine if you wish to try it out. It does not do too much though, does it?
I hope you enjoyed this exercise with Sequential Access Files, as well as enhanced your understanding of how they work. Now we can move on to Page 18.