Title: Tutorial 10 Random Access Files
1Tutorial 10Random Access Files
2Random Access FilesLesson A Objectives
- After completing this lesson, you will be able
to - Define the terminology used with random access
files - Create a record structure
- Open and close a random access file
- Write data to and read data from a random access
file - Initialize a random access file
- Test for the end of a random access file
3Random Access Files Versus Sequential Access Files
- A random access file is composed of fields and
records - A field is a single item of information about a
person, place, or thing - A record is a group of related fields that
contain all of the necessary data about a
specific person, place, or thing - Each record in a random access file also has a
unique number, called a record number, that
indicates its position in the file
4Records and Fields
- You can directly access a record in a random
access file by its record number - For this reason, random access files also are
referred to as direct access files
A set of student records
5Creating a Record Structure
- Each record in a random access file must contain
the same number of fields - You ensure that each record contains the exact
same fields by creating a record structure - The record structure itself actually becomes a
data type, called a user-defined data type, which
is separate and distinct from the standard data
types available in Visual Basic .NET - You use the Structure statement to create a
record structure in Visual Basic .NET - An attribute is simply an instruction that
provides additional information for the Visual
Basic .NET compiler
6Creating a Record Structure
- The Structure statement begins with the keyword
Structure followed by the
name of the record structure the
statement ends with the keywords EndStructure
7Declaring a Record Variable
- A record variable will itself contain one or more
variables called field variables - You separate the record variables name from the
field variables name with a period - Declare the variable
- Dim udtEmploy As EmployStruc
- Referring to the field variables
- udtEmploy.intEmployNumber
- udtEmploy.strFirstName
- udtEmploy.strLastName
- udtEmploy.sngSalary
A User-Defined Data Type
8Opening a Random Access File
- In Visual Basic .NET, you use the FileOpen
function to open either a new random access file
or an existing random access file
9Writing Records to a Random Access File
- You use the FilePut function to write a record to
a random access file
10Initializing a Random Access File
Declare a variable
Initialize field variables
Open file
Write records
11Reading Records from a Random Access File
- You use the FileGet function to read a record
from a random access file
12Testing for the End of a Random Access File
- You can use the EOF function, which stands for
end of file, to determine whether the file
pointer is at the end of a random access file
13Closing a Random Access File
- You use the FileClose function to close a random
access file
14Using a Random Access File in an
ApplicationLesson B Objectives
- After completing this lesson, you will be able
to - Use a record structure in an application
- Initialize a random access file in an application
- Add records to a random access file
- Display a specific record contained in a random
access file - Verify that the record number is valid
15Coding the InitializeButton Click Event Procedure
- Figure 10-4 shows the pseudocode for the
Initialize File buttons Click event procedure
16(No Transcript)
17Coding the AddButtonClick Event Procedure
- The AddButton Click event procedure is
responsible for adding a participant record to
the random access file - Figure 10-18 shows the pseudocode for the
AddButton Click event procedure
18(No Transcript)
19Coding the DisplayButton Click Event Procedure
- The DisplayButton Click event procedure is
responsible for displaying the record associated
with a specific seat number - Figure 10-23 shows the pseudocode for the
DisplayButton Click event procedure
20(No Transcript)
21Completing the Seminar ApplicationLesson C
Objectives
- After completing this lesson, you will be able
to - Delete the information from a record stored in a
random access file - Print the contents of a random access file
22Coding the Remaining Procedures in the Seminar
Application
- You need to code the Click event procedures for
the RemoveButton and PrintButton controls, and
the PrintPage event procedure for the
SeminarPrintDocument control
23Coding the RemoveButtonClick Event Procedure
- The RemoveButton Click event procedure is
responsible for removing a record from the
participants.data file - Figure 10-27 shows the pseudocode for the
RemoveButton Click event procedure
24(No Transcript)
25Coding the PrintButton Click Event Procedure
- The PrintButton Click event procedure is
responsible for using the SeminarPrintDocument
control to print the contents of the random
access file
26Coding the SeminarPrintDocument PrintPage Event
Procedure
- The SeminarPrintDocument controls PrintPage
event procedure is responsible for printing the
Seminar Participant Report - The report should contain three columns of
information - The first column should list 20 seat numbers, and
the second and third columns should list the
corresponding participant and company names,
which are stored in the participants.data file