Visual Basic 6 - Random Access Files

  Gaz W 02:03 16 Dec 2004

I want to create a "database" using Visual Basic and random access files. I've never done anything like this before, although I am OK-ish at VB now.

Basically it's only got to store a couple of records; First Name, Surname, Address Line 1 etc, Postcode, anything else I think of that's required.

Basically I need to be able to browse the records one by one and add them... I know how I'll go about deleting records once I know where to begin with the rest. If possible, could the fields appear in text boxes?

I know they are fixed length, which should help, and I am aware that's how you delete and find out the number of the last record to add one to it (total file length)...

Sorry I'm not explaining too well, but I don't really fully understand the idea behind using files with VB so I'm a bit stuck where to begin and what I should be asking! Any help would be much appreciated.

Thanks in advance,


  Gaz W 14:54 16 Dec 2004

Any ideas people?

  spargo 17:28 16 Dec 2004

Taken from VB6 Help Files

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:
Open pathname [For Random] As filenumber Len = 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

  spargo 17:29 16 Dec 2004

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."

  spargo 17:33 16 Dec 2004

Hope this is of some help.The formatting of the message has been altered by the forum but you should be able to follow it.

  Gaz W 20:09 16 Dec 2004

Thanks very much for your help. I've now got it doing something but I'll leave this thread open for now in case I mess it up adding functions.

Thanks again


This thread is now locked and can not be replied to.

Elsewhere on IDG sites

HTC U12 Plus review: Hands-on

Best Android emulators for Mac

TV & Streaming : comment regarder Roland Garros ?