Transcript Data Files

Random Access Files
•A Random Access file is like an array of records stored
on a disk.
•The records are numbered 1,2,3, and so on, and can be
referred to by their numbers.
•Therefore, a Random Access File resembles a box of
index cards, each having a numbered tab. Any card can
be selected from the box without first reading every
index card preceding it; similarly any record of a
random access file can be read without having to read
every record preceding it.
Random File Organization
• Records are fixed length and have a record
number (data type=long integer) for
reference
• Fields are fixed length and position
– Less data then length will be padded with
spaces
– More data than length will be truncated
Random File Example
• Read or written in any order
• Think of the structure being "like a
table"
Lynne
Jim
Tom
Weldon
Buckner
Thumb
803-649-9999
803-652-1111
803-593-1234
Defining the Record Structure
•
•
•
•
Must be done before reading or writing
Use Type/End Type statements
Code in General Declarations
Use fixed length strings
– Specify length in Dim statement
– Ex: Dim strFName as String * 20
• Number variables do not require explicit
length
Type/End Type
Private Type Person
intEmpNum As Integer
strFName
As String * 20
strLName
As String * 30
strPhone
As String * 12
curRate
As Currency
End Type
Dim mudtPersonRecord As Person
Open App.Path & "\Names.dat" For Random as #1
Len=Len (mudtPersonRecord)
Note: mudt prefix for User Defined Type
Open Statement
• Once the random file is opened it can be
used for both input and output unlike
sequential files!
• If you open a file that does not exist, VB
will create it as an empty file
• Once opened, data are available for
read/write operations one record at a time
Reading a Random File
• Open an existing file for Random
• Use Get Statement to read the records
Get Statement
• Usually coded in Form_Load
• If populating a listbox or combo use a Do
Until reach last record to read data
Get #FileNumber, [RecordNumber], RecordName
Get #2, 4, mudtPersonRecord
Get #2, intRecordNumber, mudtPersonRecord
[ ] indicates optional, if RecordNumber is omitted, the next record is read
Put Statement
• Use to place/save data in a random file
• File must already be opened "For Random"
• Place code in mnuFileSave, mnuFileClose
or Form_QueryUnload
• Follow with a Close statement
Put Statement Example
Put #FileNumber, [RecordNumber], RecordName
Put #2, 4, mudtPersonRecord
Put #2, intRecordNumber, mudtPersonRecord
[ ] indicates optional, if RecordNumber is omitted, the next
record is read
Accessing the Fields
• Get and Put operate on an entire record
• Reference the individual fields using dot
notation for the User Defined Type
Accessing the Fields (cont.)
• Read using Get then update textbox
Get #2, 1, mudtPersonRecord
txtLName.Text = mudtPersonRecord.strLName
• Update field from textbox the Write using Put:
mudtPersonRecord.strPhone = txtPhone.Text
Put #2, 1, mudtPersonRecord
LOF Function
• Length of File function returns the size of the file
in bytes
• Use instead of EOF used for sequential files
• To determine the highest record number in the file
divide LOF by the size of one record
LOF(FileNumber)
LOF(3)
intNumRecords=LOF(3)/Len(mudtPersonRecord)
Seek Function
• Returns the current location of the pointer =
the next record in the file to be processed
Seek(FileNumber)
intNextRecord=Seek(3)
Trim Functions
•
•
•
•
Remove extra blank spaces in a string
Trim ==> removes spaces from both ends
LTrim==> removes spaces at left end
RTrim ==> removes spaces at right end
Trim(String)
LTrim(String)
RTrim(String)
txtLName.Text=RTrim(mudtPersonRecord.strLName)
Reading/Retrieving Records –
Sample Code
Sub GetRecord(lngRecNum as Long)
Get #1, lngRecNum, mudtPersonRecord
With mudtPhoneRecord
txtEmpNum.Text = .intEmpNum
txtFName.Text = RTrim(.strFName)
txtLName.Text = Rtrim(.strLName)
txtPhone.Text = Rtrim(.strPhone)
txtRate.Text = .curRate
End With
End Sub
Writing Records –Sample Code
Sub PutRecord(lngRecNum as Long)
With mudtPhoneRecord
.intEmpNum=Val(txtEmpNum.Text)
.strFName = txtFName.Text
.strLName = txtLName.Text
.strPhone = txtPhone.Text
.curRate = Val(txtRate.Text)
End With
Put #1, lngRecNum, mudtPersonRecord
End Sub