340 likes | 1.16k Views
Tutorial 10 Random Access Files. Random Access Files Lesson 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
E N D
Random 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
Random 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
Records 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
Creating 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
Creating 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
Declaring a Record Variable • A record variable will itself contain one or more variables called field variables • You separate the record variable’s name from the field variable’s 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
Opening 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
Writing Records to a Random Access File • You use the FilePut function to write a record to a random access file
Initializing a Random Access File Declare a variable Initialize field variables Open file Write records
Reading Records from a Random Access File • You use the FileGet function to read a record from a random access file
Testing 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
Closing a Random Access File • You use the FileClose function to close a random access file
Using 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
Coding the InitializeButton Click Event Procedure • Figure 10-4 shows the pseudocode for the Initialize File button’s Click event procedure
Coding 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
Coding 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
Completing 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
Coding 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
Coding 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
Coding 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
Coding the SeminarPrintDocument PrintPage Event Procedure • The SeminarPrintDocument control’s 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