Also, if you read the data back into the array using the Get #intff, 1, udtType(), you will need to know how many records are in the file so you can ReDim udtType() appropriately...
Try this. I can't test it right now but it should work...just put this code into an empty form:
VB Code:
Option Explicit Private Type MYTYPE strOne As String strTwo As String strThree As String End Type Private udtType() As MYTYPE 'Turns a number like "4" into "0004" Private Function PadNumber(TheNumber As String, ByVal Length As Long) As String Dim lonLen As Long lonLen = Len(TheNumber) If lonLen = Length Then PadNumber = TheNumber Else PadNumber = String$(Length - lonLen, "0") & TheNumber End If End Function 'Turns a number like "0004" into "4" Private Function RemoveHeader(TheHeader As String) As String Dim lonLen As Long, lonLoop As Long lonLen = Len(TheHeader) For lonLoop = 1 To lonLen If Not Mid$(TheHeader, lonLoop, 1) = "0" Then RemoveHeader = Mid$(TheHeader, lonLoop) Exit Function End If Next lonLoop End Function 'Debug the array. Private Sub DebugType() Dim intLoop As Integer Dim strName As String For intLoop = 0 To UBound(udtType()) With udtType(intLoop) strName = "udtType(" & intLoop & "):" Debug.Print strName Debug.Print String$(Len(strName), "-") Debug.Print "One: " & .strOne Debug.Print "Two: " & .strTwo Debug.Print "Three: " & .strThree Debug.Print "" End With Next intLoop End Sub 'Just load some stuff into the UDT array. Private Sub InitType() Dim intLoop As Integer Randomize ReDim udtType(50) As MYTYPE For intLoop = 0 To 50 With udtType(intLoop) .strOne = "1 (" & intLoop & ")" .strTwo = CStr(Int(Rnd * 255) + 1) .strThree = CStr(Int(Rnd * 255) + 1) End With Next intLoop End Sub Private Sub Form_Load() Dim intFF As Integer Dim strHeader As String 'Load stuff into the array. InitType intFF = FreeFile strHeader = PadNumber(UBound(udtType), 4) 'Write file. Open "C:\dump.txt" For Binary Access Write As #intFF 'Write header. Put #intFF, 1, strHeader 'Write data. Put #intFF, Len(strHeader) + 1, udtType() Close #intFF DebugType 'Load file back. intFF = FreeFile strHeader = String$(4, " ") Dim lonLenData As Long Open "C:\dump.txt" For Binary Access Read As #intFF 'Read header. Get #intFF, 1, strHeader 'Read data. lonLenData = CLng(RemoveHeader(strHeader)) ReDim udtType(lonLenData) As MYTYPE 'Data always starts at 5th byte (header is 4 bytes). Get #intFF, 5, udtType() Close #intFF DebugType End Sub




Reply With Quote