M
Montana DOJ Help Desk
Word 2000
I've been working on creating my first user-defined, and using it to write
data to a file, and then to read data from the file. Here's my code (Please
excuse the extra stuff that I put in as examples for myself of how to use
the functions):
********************
Public Type Record ' Define user-defined type.
ID As Long
Name As String * 10
Text As String * 10
End Type
Public MyRecord As Record
Sub WriteRecords()
Dim Bytes As Long
Dim ElementIDSize As Long
Dim ElementNameSize As Long
Dim ElementTextSize As Long
Dim FileNumber As Long
Dim MyRecord As Record
Dim NewRecord As Long
Dim NumRecords As Long
Dim RecordNumber As Long
Dim TypeSize As Long
FileNumber = FreeFile
' LenB function examples.
TypeSize = LenB(MyRecord)
ElementIDSize = LenB(MyRecord.ID)
ElementNameSize = LenB(MyRecord.Name)
ElementTextSize = LenB(MyRecord.Text)
' Open file for random access.
Open "TESTFILE" For Random As FileNumber Len = TypeSize
' LOF function examples. Records are appended to random access files by
adding 1 to the last record number in
' the file. Therefore, finding NumRecords and setting NewRecord allows new
data to be appended to the file.
Bytes = LOF(FileNumber)
NumRecords = Bytes / TypeSize
NewRecord = NumRecords + 1
' Writes data to the file.
For RecordNumber = NewRecord To NewRecord + 4
MyRecord.ID = RecordNumber
MyRecord.Name = "My Name" & RecordNumber
MyRecord.Text = "Record" & RecordNumber
Put FileNumber, RecordNumber, MyRecord
Next
' Closes the file.
Close FileNumber
End Sub
Sub ReadRecords()
Dim CurrentRecord As Long
Dim FileNumber As Long
Dim mbMessage As String
Dim MyRecord As Record
Dim NextRecord As Long
Dim Position As Long
Dim PreviousRecord As Long
Dim TypeSize As Long
FileNumber = FreeFile
TypeSize = LenB(MyRecord)
' Opens a file for random access.
Open "TESTFILE" For Random As FileNumber Len = TypeSize
' Reads the file using the Get statement. Displays the value for each
element of each record.
For Position = 1 To 5
Get FileNumber, Position, MyRecord
mbMessage = "ID: " & MyRecord.ID & vbCr
mbMessage = mbMessage & "Name: " & Trim(MyRecord.Name) & vbCr
mbMessage = mbMessage & "Text: " & Trim(MyRecord.Text)
MsgBox mbMessage
CurrentRecord = Seek(FileNumber) - 1
NextRecord = Seek(FileNumber)
PreviousRecord = Seek(FileNumber) - 2
Next
' Closes the file.
Close FileNumber
End Sub
********************
This all works. However, it bothers me a little that I need to pad the
string values in my user-defined type (i.e. Name As String * 10) in order to
make it work. If I remove the "* 10" from the user-defined type
declaration, then the PUT statement generates:
Run-time error '59':
Bad record length
Is there any way to have variable length strings--and therefore, variable
length records--without running into the above error, or am I stuck with
using fixed length strings?
I suspect that the answer to my question is "No," or, "Yes, but it's so much
work that it's not worth it." Thought I'd ask the question anyway.
-- Tom
State of Montana
Department of Justice Help Desk
"Making the world a safer place."
I've been working on creating my first user-defined, and using it to write
data to a file, and then to read data from the file. Here's my code (Please
excuse the extra stuff that I put in as examples for myself of how to use
the functions):
********************
Public Type Record ' Define user-defined type.
ID As Long
Name As String * 10
Text As String * 10
End Type
Public MyRecord As Record
Sub WriteRecords()
Dim Bytes As Long
Dim ElementIDSize As Long
Dim ElementNameSize As Long
Dim ElementTextSize As Long
Dim FileNumber As Long
Dim MyRecord As Record
Dim NewRecord As Long
Dim NumRecords As Long
Dim RecordNumber As Long
Dim TypeSize As Long
FileNumber = FreeFile
' LenB function examples.
TypeSize = LenB(MyRecord)
ElementIDSize = LenB(MyRecord.ID)
ElementNameSize = LenB(MyRecord.Name)
ElementTextSize = LenB(MyRecord.Text)
' Open file for random access.
Open "TESTFILE" For Random As FileNumber Len = TypeSize
' LOF function examples. Records are appended to random access files by
adding 1 to the last record number in
' the file. Therefore, finding NumRecords and setting NewRecord allows new
data to be appended to the file.
Bytes = LOF(FileNumber)
NumRecords = Bytes / TypeSize
NewRecord = NumRecords + 1
' Writes data to the file.
For RecordNumber = NewRecord To NewRecord + 4
MyRecord.ID = RecordNumber
MyRecord.Name = "My Name" & RecordNumber
MyRecord.Text = "Record" & RecordNumber
Put FileNumber, RecordNumber, MyRecord
Next
' Closes the file.
Close FileNumber
End Sub
Sub ReadRecords()
Dim CurrentRecord As Long
Dim FileNumber As Long
Dim mbMessage As String
Dim MyRecord As Record
Dim NextRecord As Long
Dim Position As Long
Dim PreviousRecord As Long
Dim TypeSize As Long
FileNumber = FreeFile
TypeSize = LenB(MyRecord)
' Opens a file for random access.
Open "TESTFILE" For Random As FileNumber Len = TypeSize
' Reads the file using the Get statement. Displays the value for each
element of each record.
For Position = 1 To 5
Get FileNumber, Position, MyRecord
mbMessage = "ID: " & MyRecord.ID & vbCr
mbMessage = mbMessage & "Name: " & Trim(MyRecord.Name) & vbCr
mbMessage = mbMessage & "Text: " & Trim(MyRecord.Text)
MsgBox mbMessage
CurrentRecord = Seek(FileNumber) - 1
NextRecord = Seek(FileNumber)
PreviousRecord = Seek(FileNumber) - 2
Next
' Closes the file.
Close FileNumber
End Sub
********************
This all works. However, it bothers me a little that I need to pad the
string values in my user-defined type (i.e. Name As String * 10) in order to
make it work. If I remove the "* 10" from the user-defined type
declaration, then the PUT statement generates:
Run-time error '59':
Bad record length
Is there any way to have variable length strings--and therefore, variable
length records--without running into the above error, or am I stuck with
using fixed length strings?
I suspect that the answer to my question is "No," or, "Yes, but it's so much
work that it's not worth it." Thought I'd ask the question anyway.
-- Tom
State of Montana
Department of Justice Help Desk
"Making the world a safer place."