Visual Basic Concepts
Using Random File Access
The File System Object model does not provide random file creation or access methods. If you need to create or read random files, this information will help you do so.
The bytes in random-access files form identical records, each containing one or more fields. A record with one field corresponds to any standard type, such as an integer or fixed-length string. A record with more than one field corresponds to a user-defined type. For example, the Worker Type defined below creates 19-byte records that consist of three fields:
Type Worker
LastName As String * 10
Title As String * 7
Rank As String * 2
End Type
Declaring Variables
Before your application opens a file for random access, it should declare all variables required to handle data from the file. This includes user-defined types, which correspond to records in the file, as well as standard types for other variables that hold data related to processing a file opened for random access.
Defining Record Types
Before opening a file for random access, define a type that corresponds to the records the file does or will contain. For example, an Employee Records file could declare a user-defined data type called Person
as follows:
Type Person
ID As Integer
MonthlySalary As Currency
LastReviewDate As Long
FirstName As String * 15
LastName As String * 15
Title As String * 15
ReviewComments As String * 150
End Type
Declaring Field Variables in a Type Definition
Because all records in a random-access file must have the same length, it is often useful for string elements in a user-defined type to have a fixed length, as shown in the Person
type declaration above, where, for instance, FirstName
and LastName
have a fixed length of 15 characters.
If the actual string contains fewer characters than the fixed length of the string element to which it is written, Visual Basic fills the trailing spaces in the record with blanks (character code 32). Also, if the string is longer than the field size, it is truncated. If you use variable-length strings, the total size of any record stored with Put or retrieved with Get must not exceed the record length specified in the Open statement’s Len clause.
Declaring Other Variables
After defining a type that corresponds to a typical record, declare any other variables that your application needs to process a file opened for random access. For example:
' A record variable.
Public Employee As Person
' Tracks the current record.
Public Position As Long
' The number of the last record in the file.
Public LastRecord As Long
Opening Files for Random Access
To open a file for random access, use the following syntax for the Open statement:
Openpathname [For Random] AsfilenumberLen = reclength
Because Random is the default access type, the For Random keywords are optional.
The expression Len = reclength specifies the size of each record in bytes. Note that every string variable in Visual Basic stores a Unicode string and that you must specify the byte length of that Unicode string. If reclength is less than the actual length of the record written to the file, an error is generated. If reclength is greater than the actual length of the record, the record is written, although some disk space may be wasted.
You could use the following code to open a file:
Dim FileNum As Integer, RecLength As Long, Employee As Person
' Calculate the length of each record.
RecLength = LenB(Employee)
' Get the next available file number.
FileNum = FreeFile
' Open the new file with the Open statement.
Open "MYFILE.FIL" For Random As FileNum Len = RecLength
Editing Files Opened for Random Access
If you want to edit a random access file, first read records from the file into program variables, then change the values in the variables, and finally, write the variables back into the file. The following sections discuss how to edit files opened for random access.
Reading Records into Variables
Use the Get statement to copy records into variables. For instance, to copy a record from the Employee Records file into the Employee
variable, you could use the following code:
Get FileNum, Position, Employee
In this line of code, FileNum
contains the number that the Open statement used to open the file; Position
contains the record number of the record to copy; and Employee
, declared as user-defined type Person
, receives the contents of the record.
Writing Variables to Records
Use the Put statement to add or replace records into files opened for random access.
Replacing Records
To replace records, use a Put statement, specifying the position of the record you want to replace; for example:
Put #FileNum, Position, Employee
This code will replace the record number specified by Position
, with the data in the Employee
variable.
Adding Records
To add new records to the end of a file opened for random access, use the Put statement shown in the preceding code fragment. Set the value of the Position
variable equal to one more than the number of records in the file. For example, to add a record to a file that contains five records, set Position
equal to 6.
The following statement adds a record to the end of the file:
LastRecord = LastRecord + 1
Put #FileNum, LastRecord, Employee
Deleting Records
You could delete a record by clearing its fields, but the record would still exist in the file. Usually you don’t want empty records in your file, because they waste space and interfere with sequential operations. It is better to copy the remaining records to a new file, and then delete the old file.
To remove a deleted record in a random-access file
Create a new file.
Copy all the valid records from the original file into the new file.
Close the original file and use the Kill statement to delete it.
Use the Name statement to rename the new file with the name of the original file.
For More Information For additional information on random file access, see "Open Statement."