Files and Folders

Random file access

A Random Access file is like a database. It is made up of records of identical size. Each record is made up of fields that store data. You can use a Random Access file to store data from your application, like multiple addresses or Templates.

To use the following code, create 3 text boxes called txtAddress, txtName, txtPhone. Create a label called lblRecCount and 3 Command Button called cmdNext, cmdLast and cmdGotoRecord. Then enter the following code into your form code section. Take a look at the comments in the code to see how it all works.

' Add this to a module
Option Explicit
' Declare the record structure
Public Type PersonInfo
    Name As String * 40 ' Name String (40 character string)
    Phone As String * 40 ' Phone String (40 character string)
    Address As String * 50 ' Address String 50 character string)
End Type


'Add this to the form
Option Explicit
'The variable gPerson consists of the three variables contained in PersonInfo
Dim Person As PersonInfo
Dim lRecordLen As Long
Dim nFileNum As Integer
Dim lLastRecord As Long
Dim lCurrentRecord As Long
Private Sub Form_Load()
    ' Get the length of a record
    lRecordLen = Len(Person)
    ' Get a free file number
    nFileNum = FreeFile
    ' Open the file storing the information, if it does not exist create it
    Open App.Path & "Address.dat" For Random As nFileNum Len = lRecordLen
    ' Update the current record
    lCurrentRecord = 1
    ' Get the last record
    lLastRecord = FileLen(nFileNum)  / lRecordLen
    ' If the file has just been created update LastRecord
    If lLastRecord = 0 Then lLastRecord = 1
    ' Fill the textboxes with record1
    ShowCurrentRecord
End Sub
Private Sub Form_Unload(Cancel As Integer)
    ' Close the file
    Close nFileNum
End Sub
Sub ShowCurrentRecord()
    If lCurrentRecord = 0 Then lCurrentRecord = 1
    ' This procedure fills the textboxes with the data
    Get #nFileNum, lCurrentRecord, Person
    ' Display the information
    txtName = Trim(Person.Name)
    txtAddress = Trim(Person.Address)
    txtPhone = Trim(Person.Phone)
    lblRecCount = "Record " & Str(lCurrentRecord)
End Sub

Sub SaveCurrentRecord()
    ' Fill gPerson from text boxes
    Person.Name = txtName
    Person.Phone = txtPhone
    Person.Address = txtAddress
    ' Save the data to the file
    Put #nFileNum, lCurrentRecord, Person
End Sub
Sub cmdGoToRecord_Click()
    Dim sResult As String
    ' Save the current record
    SaveCurrentRecord
    sResult = InputBox ("Enter the record number to go to")
    ' If cancel was pressed
    If sResult = "" Then
        Exit Sub
    ElseIf Not IsNumeric(sResult) Then
        ' A Number was not entered
        Msgbox "Please enter a number"
    Else
        lCurrentRecord = sResult
        ShowCurrentRecord
    End If
End Sub

Private Sub cmdNext_Click()
    ' Save the current record
    SaveCurrentRecord
    If lCurrentRecord = lLastRecord + 1 Then Exit Sub
    ' go next
    lCurrentRecord = lCurrentRecord + 1
    ShowCurrentRecord
End Sub
Private Sub cmdLast_Click()
    ' Save the current record
    SaveCurrentRecord
    If lCurrentRecord = 1 Then Exit Sub
    ' go next
    lCurrentRecord = lCurrentRecord - 1
    ShowCurrentRecord
End Sub

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“C++ : Where friends have access to your private members.” - Gavin Russell Baker