Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleWork with random access records in a file
Keywordsrandom access, file, open
CategoriesFiles and Directories
 
Open the file for Random access, specifying the length of its records. Use Get to fetch records and Put to save them.

This example reads and writes all records when it loads and unloads but you can read a record in any position in the file at any time.

 
Private Type NoteRecord
    Date1 As String * 6
    Who As String * 10
    From As String * 10
    Note As String * 60
    Id As String * 6
    Date2 As String * 6
End Type

' Load the records.
Private Sub Form_Load()
Dim i As Integer
Dim file_name As String
Dim fnum As Integer
Dim note_record As NoteRecord

    ' Load controls.
    For i = 1 To 9
        Load txtDate1(i): txtDate1(i).Move txtDate1(i - _
            1).Left, txtDate1(i - 1).Top + txtDate1(i - _
            1).Height, txtDate1(i - 1).Width, txtDate1(i - _
            1).Height: txtDate1(i).Visible = True
        Load txtWho(i): txtWho(i).Move txtWho(i - 1).Left, _
            txtDate1(i).Top, txtWho(i - 1).Width, txtWho(i _
            - 1).Height: txtWho(i).Visible = True
        Load txtFrom(i): txtFrom(i).Move txtFrom(i - _
            1).Left, txtDate1(i).Top, txtFrom(i - 1).Width, _
            txtFrom(i - 1).Height: txtFrom(i).Visible = True
        Load txtNote(i): txtNote(i).Move txtNote(i - _
            1).Left, txtDate1(i).Top, txtNote(i - 1).Width, _
            txtNote(i - 1).Height: txtNote(i).Visible = True
        Load txtId(i): txtId(i).Move txtId(i - 1).Left, _
            txtDate1(i).Top, txtId(i - 1).Width, txtId(i - _
            1).Height: txtId(i).Visible = True
        Load txtDate2(i): txtDate2(i).Move txtDate2(i - _
            1).Left, txtDate1(i).Top, txtDate2(i - _
            1).Width, txtDate2(i - 1).Height: _
            txtDate2(i).Visible = True
        Load cmdDelete(i): cmdDelete(i).Move cmdDelete(i - _
            1).Left, txtDate1(i).Top, cmdDelete(i - _
            1).Width, cmdDelete(i - 1).Height: _
            cmdDelete(i).Visible = True
    Next i

    ' Open the data file.
    file_name = App.Path
    If Right$(file_name, 1) <> "\" Then file_name = _
        file_name & "\"
    file_name = file_name & "records.txt"
    fnum = FreeFile
    Open file_name For Random As fnum Len = Len(note_record)

    ' Read the records.
    For i = 0 To 9
        Get #fnum, i + 1, note_record
        txtDate1(i).Text = Trim$(note_record.Date1)
        txtWho(i).Text = Trim$(note_record.Who)
        txtFrom(i).Text = Trim$(note_record.From)
        txtNote(i).Text = Trim$(note_record.Note)
        txtId(i).Text = Trim$(note_record.Id)
        txtDate2(i).Text = Trim$(note_record.Date2)
    Next i

    ' Close the file.
    Close #fnum
End Sub

' Save the modified records.
Private Sub Form_Unload(Cancel As Integer)
Dim i As Integer
Dim file_name As String
Dim fnum As Integer
Dim note_record As NoteRecord

    ' Open the data file.
    file_name = App.Path
    If Right$(file_name, 1) <> "\" Then file_name = _
        file_name & "\"
    file_name = file_name & "records.txt"
    fnum = FreeFile
    Open file_name For Random As fnum Len = Len(note_record)

    ' Save the records.
    For i = 0 To 9
        note_record.Date1 = txtDate1(i).Text
        note_record.Who = txtWho(i).Text
        note_record.From = txtFrom(i).Text
        note_record.Note = txtNote(i).Text
        note_record.Id = txtId(i).Text
        note_record.Date2 = txtDate2(i).Text
        Put #fnum, i + 1, note_record
    Next i

    ' Close the file.
    Close #fnum
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated