This documentation is archived and is not being maintained.

Writing Variables to Records

Visual Studio .NET 2003

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 Position with the data in the Employee variable.

To add a record

  • Using FilePut, set the value of the Position variable 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 Position equal 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 Position:

    LastRecord = LastRecord + 1
    FilePut(FileNum, Employee, LastRecord)

Deleting Records

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

  1. Create a new file.
  2. Copy all the valid records from the original file into the new file.
  3. Close the original file and use the Kill function to delete it.
  4. Use the Rename function to rename the new file with the name of the original file.

See Also

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