Random Access Files Dr. John Abraham
Advantages of Random Access Access one record without disturbing others Modify that record Create Index if needed to access it many ways
Structure Module modulePersonRec Structure personRecord <VBFixedString(7)> Public RecordNumber As String <VBFixedString(9)> Public SS As String <VBFixedString(40)> Public Name As String <VBFixedString(10)> Public Tele As String <VBFixedString(2)> Public LineBreak As String End Structure
Using Structure Public DATAFILE As String = "mydb.mdf" Public Person As personRecord Public Position As Long Public LastRecord As Long Public FileLength As Long Public indexSS(100) As String Public indexKey(100) As Long End Module
Opening a Random file Private Sub btnOpen_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click Position = 1 ' Calculate the record length. RecLength = Len(Person) FileOpen(1, DATAFILE, OpenMode.Random, , , RecLength) FileLength = LOF(1) LastRecord = FileLength / RecLength lblConnected.Text = "Connected... Total " & LastRecord & " Records Found. " lblRecNo.Text = LastRecord + 1 End Sub
Writing to a Random File Person.SS = txtSSN.Text Person.Name = txtName.Text Person.Tele = txtTele.Text Person.LineBreak = Chr(13) & Chr(10) LastRecord = LOF(1) / RecLength Person.RecordNumber = LastRecord + 1 'If LastRecord > 0 Then findLink(link) FilePut(1, Person, LastRecord + 1)
Reading from a Random File FileGet(1, Person, Val(txtKey.Text)) txtSSN.Text = Person.SS txtName.Text = Person.Name txtTele.Text = Person.Tele
Reading one after another (scanning) Private Sub tabScan_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles tabScan.Click Position = 1 lblPosition.Text = Position FileGet(1, Person, Position) lblSS.Text = Person.SS lblName.Text = Person.Name lblTele.Text = Person.Tele End Sub
Displaying a record Dim tmps As String With Person lblPosition.Text = Person.RecordNumber tmps = Trim(Person.SS) lblSS.Text = tmps.Substring(0, 3) & "-" & tmps.Substring(3, 2) & "-" & tmps.Substring(5) lblName.Text = Trim(Person.Name) tmps = Trim(Person.Tele) lblTele.Text = tmps.Substring(0, 3) & "-" & tmps.Substring(3, 3) & "-" & tmps.Substring(6) End With
Linear Search For i = 1 To LOF(1) / RecLength FileGet(1, Person, i) If Person.Name.IndexOf(txtNameSearch.Text) > -1 Then lblNameS.Text = Person.Name lblTeleS.Text = Person.Tele lblSSnS.Text = Person.SS lblRecS.Text = i Exit For Else lblNameS.Text = "Not Found" lblTeleS.Text = "" lblSSnS.Text = "" lblRecS.Text = 0 End If
Creating and Index on SS LastRecord = LOF(1) / RecLength For i = 1 To LastRecord FileGet(1, Person, i) indexSS(i) = Person.SS indexKey(i) = i Next
Sorting Index i = LastRecord Do sorted = True For j = 1 To i - 1 If indexSS(j) > indexSS(j + 1) Then sorted = False temp1 = indexSS(j) : temp2 = indexKey(j) indexSS(j) = indexSS(j + 1) : indexKey(j) = indexKey(j + 1) indexSS(j + 1) = temp1 : indexKey(j + 1) = temp2 End If Next j i = i - 1 Loop While Not sorted ' (sorted = False) MessageBox.Show("Index Sorted.", "Sort")
Binary Search Lookfor = Trim(txtSSnS.Text) nfound = False first = 1 : last = LastRecord While (Not (nfound) And first <= last) middle = (first + last) \ 2 MessageBox.Show(Lookfor & " " & indexSS(middle), "Binary search demonstration ") If Lookfor = Trim(indexSS(middle)) Then nfound = True FileGet(1, Person, indexKey(middle)) lblNameS.Text = Person.Name lblTeleS.Text = Person.Tele lblSSnS.Text = Person.SS lblRecS.Text = indexKey(middle) ElseIf Lookfor < indexSS(middle) Then last = middle - 1 Else first = middle + 1 End If End While If Not (nfound) Then lblNameS.Text = "Not Found!"