Option Explicit
' define structure of a variable-length record
Type Person_VariableLength
ID As Integer
MonthlySalary As Currency
LastReviewDate As Long
FirstName As String
LastName As String
Title As String
ReviewComments As String
End Type
' define structure of a fixed-length record
Type Person_FixedLength
ID As Integer
MonthlySalary As Currency
LastReviewDate As Long
FirstName As String * 15
LastName As String * 15
Title As String * 15
ReviewComments As String * 150
End Type
' variables for sample data
Private binSampleOne As Person_VariableLength
Private binSampleTwo As Person_VariableLength
Private randomSampleOne As Person_FixedLength
Private randomSampleTwo As Person_FixedLength
' record number for use with random (fixed-length) file
Private intRecNum As Integer
Public Sub main()
buildSampleRecords
demonstrateBinary
demonstrateRandom
End Sub
Private Function demonstrateBinary()
' open the file and write the records
Open "c:\BINsample" For Binary As #1
Put #1, , binSampleOne
Put #1, , binSampleTwo
Close #1
' open the file and read the records
Open "c:\binSample" For Binary As #1
Dim udtPerson As Person_VariableLength
Do While Not EOF(1)
Get #1, , udtPerson
' display selected values
With udtPerson
Debug.Print .FirstName
Debug.Print .LastName
Debug.Print .ReviewComments
End With
Loop
Close #1
End Function
Private Function demonstrateRandom()
' open the file and write the records
Dim recSize As Person_FixedLength
Open "c:\Randomsample" For Random As #1 Len = Len(recSize)
Put #1, 1, randomSampleOne
Put #1, 2, randomSampleTwo
Close #1
'open the file and read the records
Dim udtPerson As Person_FixedLength
Open "c:\randomsample" For Random As #1 Len = Len(udtPerson)
' demonstrate random access capability by reading file backwards
For intRecNum = 2 To 1 Step -1
Get #1, intRecNum, udtPerson
' display selected values
With udtPerson
Debug.Print .FirstName
Debug.Print .LastName
Debug.Print .ReviewComments
End With
Next intRecNum
Close #1
End Function
Private Sub buildSampleRecords()
' routine to build sample records
' note that populating the data of the User Defined Type is the
' same regardless of fixed or variable length members
With binSampleOne
.ID = 1
.MonthlySalary = 50000
.LastReviewDate = #2/2/2000#
.FirstName = "Wilma"
.LastName = "Flintstone"
.Title = "Movie Star"
.ReviewComments = "Contract renewed for sequel"
End With
With binSampleTwo
.ID = 2
.MonthlySalary = 50000
.LastReviewDate = #2/2/2000#
.FirstName = "Fred"
.LastName = "Flintstone"
.Title = "Movie Star"
.ReviewComments = "Replace with Clint Eastwood for sequel"
End With
With randomSampleOne
.ID = 1
.MonthlySalary = 50000
.LastReviewDate = #2/2/2000#
.FirstName = "Wilma"
.LastName = "Flintstone"
.Title = "Movie Star"
.ReviewComments = "Contract renewed for sequel"
End With
With randomSampleTwo
.ID = 2
.MonthlySalary = 50000
.LastReviewDate = #2/2/2000#
.FirstName = "Fred"
.LastName = "Flintstone"
.Title = "Movie Star"
.ReviewComments = "Replace with Clint Eastwood for sequel"
End With
End Sub