How to Create a Random Access File

Random access files are generated in the Visual Basic programming language to give the programmer the ability to read or write data anywhere in the file. For most common files, the application reads the file from beginning to the end in the order in which the file is written. Random access files are read by record, so they can be read in any order. Creating and writing a file using Visual Basic requires only a few steps.

Instructions

    • 1

      Define a variable for the record type. In this example, the class "customer" is used to populate the file created in the next steps.
      Dim C as Customer

    • 2

      Create the file using the "open" statement. This file directive uses the variable created in Step 1.
      Open "myRand.ran" For Random As #1 Len = Len(C)

    • 3

      Create a record. The following statement writes a record to the file. The record is placed in the first position. You can add as many records as you want while the file is open.
      C.Last_Name = "Smith"
      C.First_Name = "Joe"
      C.State = "FL"
      Put #1, , C

    • 4

      Close the file. Before exiting the application process, close the file to free resources.
      Close #1

Related Searches:

References

Comments

You May Also Like

Related Ads

Featured