Now that we have covered the basics of binary access, we can move on to its main uses. One of the great things about binary access is not only that we can store strings and numbers, but arrays, UDTs (User-Defined Types), and variants too. This is immensly useful if we need to store an array of UDTs, which, for example contained a collection of items which stored a name, keywords, an authorid and url. For example, imagine we had the following UDT:
Private Type Resources
Name As String
Keywords As String
AuthorID As Long
URL As String
End Type
and had an array containing a number of these items...
Private cResources() As Resources
Using binary access, it is easy to save, and restore the items in this array to a file. Along with this, we could also store data such as the category that all these items applied to, and a unique id of the application that saved it (this serves as a useful check that the file we are opening is in the format we expect it to be in). These two bits of data could be stored in the following variables:
Private sCategory As String
Private sAppIdent As String * 5
Private nAppVersion As Integer
Note that in this case we use * 5 after the variable sAppIdent to specify that this string is always going to be 5 characters in length.
Now lets take a look at the code we are going to need to save it. First, in the Form_Load procedure, we need to set the other information for sCategory and sAppIdent, and then create a FillData procedure to fill the array with some dummy items:
Private Sub Form_Load()
sAppIdent = "DVPAD"
nAppVersion = App.Major
End Sub
Private Sub FillData()
ReDim cResources (1 To 2)
With cResources(1)
.Name = "Example
1"
.Keywords = "winsock,
ftp"
.AuthorID = 1
.URL = "http://vbweb.co.uk/"
End With
With cResources(2)
.Name = "Example
2"
.Keywords = "webbrowser,
ie "
.AuthorID = 3
.URL = "http://codehound.com/"
End With
sCategory = "Internet"
End Sub
Next, we can create a procedure to save this data to disk:
Private Sub SaveData()
Dim nFileNum As Integer
Dim nLen As Integer, i As Long, lCount As Long
On Error Resume Next
'delete any existing file
Kill App.Path & "example.bin"
On Error Goto 0
nFileNum = FreeFile
Open App.Path & "example.bin" For Binary Access _
Write Lock Read Write As #nFileNum
'output app id
Put #nFileNum, , sAppIdent
'output major version
Put #nFileNum, , nAppVersion
'get the length of the category
nLen = Len(sCategory)
'output them both...
Put #nFileNum, , nLen
Put #nFileNum, , sCategory
'save the number of items in array
lCount = UBound(cResources)
Put #nFileNum, , lCount
'save the array
For i = 1 To lCount
Put #nFileNum, , cResources(i)
Next i
Close #nFileNum
End Sub
and then some code to read all the data back again...
Private Sub ReadData()
Dim nFileNum As Integer, lCount As Long
Dim sFileAppIdent As String * 5, nFileAppVersion As Integer
Dim nLen As Integer, i As Long
nFileNum = FreeFile
Open App.Path & "example.bin" For Binary Access _
Read Lock Read Write As #nFileNum
'read app id.. we don't need to
'initialize string because we declared it As String * 5
'so it is always 5 chars
Get #nFileNum, , sFileAppIdent
'verify that this is correct, if not
'we are reading an invalid file
If sFileAppIdent = sAppIdent Then
'check major version
Get #nFileNum, , nFileAppVersion
If nFileAppVersion < nAppVersion Then
'we may need to use an function
to convert
'to a newer format...
ElseIf nFileAppVersion > nAppVersion Then
'produced by a later version...
'we might have trouble reading
it
End If
'get the length of the category
Get #nFileNum, , nLen
sCategory = Space$(nLen)
'get category
Get #nFileNum, , sCategory
'get num of items in array
Get #nFileNum, , lCount
If lCount > 0 Then
'resize array
ReDim cResources(1 To lCount)
'read items into array
For i = 1 To lCount
Get
#nFileNum, , cResources(i)
Next i
End If
Else
Msgbox "Invalid File"
End If
'that's it!
Close #nFileNum
'stop, so we can take a look in the Locals window
Stop
End Sub
Now, add 3 buttons to your form, and make each one call one of the procedures. Next, run your project, click the button which calls FillData. This fills the array with dummy items. Next, click the button which calls SaveData. This saves the items in the array to a file. Now, close your application. Start it up again, and this time click the button which calls LoadData. This will have filled the array using the data in the file, and paused VB at the Stop statement. Finally, click View|Locals. If you expand the object 'Me', and then 'cResources', you will be able to see the items re-loaded into the array.
Now you can create your own file formats to save your application data to a binary file....
Comments