Writing Variables to Records
After editing records read into program variables from a random-access file, use the FilePut Function to replace or add records. In Declaring Variables for Random File Access and Opening Files for Random Access, a hypothetical Employee Records database defined variables for reading and writing employee records. The following steps describe how to replace and add employee records using those variables.
To replace a record
- Use FilePut to specify the position of the record you want to replace. The Random File Access example illustrates this in the following code:
FilePut(FileNum, Employee, Position)
This code replaces the record number specified by
Positionwith the data in the
To add a record
- Using FilePut, set the value of the
Positionvariable equal to one more than the number of records in the file. To add a record to a file that contains five records, for example, set
Positionequal to 6.
In the case of the Employee Records example, substituting the following statements for the FilePut statement above adds a record to the end of the file instead of overwriting the one specified by
LastRecord = LastRecord + 1 FilePut(FileNum, Employee, LastRecord)
You can delete a record's contents by clearing its fields, but the record will still exist in the file. In most cases, you don't want empty records in your file because they waste space. To avoid this, you can copy the remaining records to a new file, and then delete the old one; or, you can make a note of the location of the empty record and use that slot on the next insert operation.
To delete a record
- Clear the record's fields.
To remove a deleted record
- 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 function to delete it.
- Use the Rename function to rename the new file with the name of the original file.
Declaring Variables for Random File Access | Opening Files for Random Access | Editing Files Opened for Random Access | File Access with Visual Basic Run-Time Functions | Sequential File Access | Binary File Access | Kill Function | Rename Function | FilePut Function