UdtToArray




Private Sub Form_Load()
Dim buffer As BookType
Dim file_length As Long
Dim file_name As String
Dim fnum As Integer
Dim i As Integer

' Get the file name.

file_name = App.Path
If Right$(file_name, 1) <> "\" Then file_name = file_name & "\"
file_name = file_name & "Books.dat"

' Get the file's size and allocate room.

file_length = FileLen(file_name)
m_NumBookData = file_length / Len(buffer)
ReDim m_BookData(0 To m_NumBookData - 1)

' Load the book information.

fnum = FreeFile
Open file_name For Binary As fnum Len = file_length
Get #fnum, 1, m_BookData
Close fnum

' List the titles.

For i = 0 To m_NumBookData - 1
lstTitles.AddItem m_BookData(i).Title
' Save the index in the data array.

lstTitles.ItemData(lstTitles.NewIndex) = i
Next i

' Display the first record.

m_SelectedRecord = -1
lstTitles.ListIndex = 0
End Sub

To load the data, open the file for Binary access specifying
the file's entire size as the record length. Allocate an array
big enough to hold all the data. Then use the Get statementh
to load all the data at once into the array.

To save the data, reverse these steps using the Put statement.











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