GetDataFile




Option Explicit
Type MyData
ID As Integer
Name As String * 15
Password As String * 15
End Type
' Do not change this.

Type RAFRecord
RecordNumber As Integer
End Type
Global UActive As Integer
Global UFile As String
Global ULength As Integer
Global RFile As String
Global RLength As Integer
'Set this in RAFStartup to the

'maximum number. 10000 is good.

Global MaxRecords As Integer
Public Sub RAFClear()
Dim RChannel As Integer
Dim i As Integer
Dim TempRAF As RAFRecord
TempRAF.RecordNumber = -1
RChannel = FreeFile
Open RFile For Random As RChannel Len = RLength
For i = 1 To MaxRecords
Put RChannel, i, TempRAF
Next i
Close RChannel
End Sub

Public Sub RAFStartup()
Dim UChannel As Integer
Dim RChannel As Integer
Dim TempRAF As RAFRecord
Dim TempMyData As MyData
UFile = App.Path & "\Users.dat"
RFile = App.Path & "\UPointer.dat"
'UFile = "C:\Users.dat"

'RFile = "C:\UPointer.dat"

ULength = Len(TempMyData)
RLength = Len(TempRAF)
UChannel = FreeFile
Open UFile For Random As UChannel Len = ULength
Close UChannel
RChannel = FreeFile
Open RFile For Random As RChannel Len = RLength
Close RChannel
UActive = (FileLen(UFile) / ULength) + 1
MaxRecords = 10000
Form1.Text1.Text = UActive
End Sub

' The parameters will need to be changed

' when you change your MyData type.

Public Function RAFAdd(ID As Integer, AddName As String, _
AddPassword As String) As Boolean
Dim UChannel As Integer
Dim RChannel As Integer
Dim TempRAF As RAFRecord
Dim NewRAF As RAFRecord
Dim TempMyData As MyData
Dim RecordPosition As Integer
Dim HashPos As Integer
Dim Done As Boolean
Dim Success As Boolean
Dim StartPos As Integer
'Add your parameters to the TempMyData here.

TempMyData.ID = ID
TempMyData.Name = AddName
TempMyData.Password = AddPassword
RecordPosition = UActive
NewRAF.RecordNumber = RecordPosition
UChannel = FreeFile
Open UFile For Random As UChannel Len = ULength
Put UChannel, RecordPosition, TempMyData
Close UChannel
HashPos = (ID Mod MaxRecords) + 1
StartPos = HashPos
Success = True
Done = False
RChannel = FreeFile
Open RFile For Random As RChannel Len = RLength
Do While Done = False
Get RChannel, HashPos, TempRAF
If TempRAF.RecordNumber = -1 Then
Done = True
Else
If HashPos = MaxRecords Then
HashPos = 1
Else
HashPos = HashPos + 1
End If
If HashPos = StartPos Then
Done = True
Success = False
End If
End If
Loop
Close RChannel
If Success Then
RChannel = FreeFile
Open RFile For Random As RChannel Len = RLength
Put RChannel, HashPos, NewRAF
Close RChannel
UActive = UActive + 1
RAFAdd = True
Else
RAFAdd = False
End If
End Function

Public Function RAFSearch(ID As Integer) As MyData
Dim UChannel As Integer
Dim RChannel As Integer
Dim TempRAF As RAFRecord
Dim TempMyData As MyData
Dim RecordPosition As Integer
Dim HashPos As Integer
Dim Done As Boolean
Dim Success As Boolean
Dim StartPos As Integer
HashPos = (ID Mod MaxRecords) + 1
StartPos = HashPos
Success = True
Done = False
RChannel = FreeFile
Open RFile For Random As RChannel Len = RLength
UChannel = FreeFile
Open UFile For Random As UChannel Len = ULength
Do While Done = False
Get RChannel, HashPos, TempRAF
If TempRAF.RecordNumber <> -1 Then
Get UChannel, TempRAF.RecordNumber, TempMyData
End If
If TempMyData.ID = ID Then
Done = True
Else
If HashPos = MaxRecords Then
HashPos = 1
Else
HashPos = HashPos + 1
End If
If HashPos = StartPos Then
Done = True
Success = False
End If
End If
Loop
Close RChannel
Close UChannel
If Not Success Then
TempMyData.ID = -1
End If
RAFSearch = TempMyData
End Function

Inputs:
RAFSearch, used to get data returns the
MyData variable type. You need To pass a
parameter For the ID to find. RAFAdd needs
To be passed the ID of the new record, and
as it currently stands, the Name and Password
of the 'user' being added. This will need to
be changed to suit you.

Returns:
RAFSearch returns the MyData variable type.
Therefore you should do:

Dim TempData as MyData
TempData = RAFSearch(50)
TempData will now have the data For ID 50.
If TempData.ID = -1, the record does Not exist.
RAFAdd returns a boolean.
True If it was added successfully,
False if it was Not -
probably a full file, increase the MaxRecords
variable and start again.

Side Effects:
The MaxRecords can only bechanged once, when
the RAFClear is executed, which clears and sets
up the pointer file.


Instructions:
Put 'RAFStartup' in Form_Load
MaxRecords will need to be changed to
the maximum number of records.
Default = 10000
RAFClear' will need to be called once,
to set up the pointer file.
Call 'RAFAdd' to add a record.
Call 'RAFSearch' to return a record.
Change this type's fields for your needs.












( getdatafile.html )- by Paolo Puglisi - Modifica del 17/12/2023