2 Attachment(s)
[VB6] - Save/Load UDT Arrays Easily
This is an example of saving a UDT array directly to disk, and then reading it back into the UDT array. It shows how to save/load UDT arrays that aren't fixed in size.
This code can be useful to store settings or any other kind of information without having to worry about parsing files.
Re: [VB6] - Save/Load UDT Arrays Easily
I was going over this and it works great.
You said in the comments that it should be able to change the max amount of items (the header part) from 4 characters (9999) to whatever.
I tried changing it to 5 characters long, and I modified all the relevant code, however it didn't work anymore.
Also I noticed this:
Code:
Private Function BuildHeader(ByVal Length As Long) As String
BuildHeader = String$(Abs(4 - Len(CStr(UBound(udtTest())))), "0") & CStr(UBound(udtTest()))
End Function
Shouldn't this be
Code:
Private Function BuildHeader(ByVal Length As Long) As String
BuildHeader = String$(Abs(Length - Len(CStr(UBound(udtTest())))), "0") & CStr(UBound(udtTest()))
End Function
because otherwise you're not making any use of the Length parameter as far as I can see.
Any suggestions ?
I changed all 4's to a 5 and all 5's to a 6, and it saves properly, but doesn't load.
I even changed 4 to Length and it still wouldn't load after another save.
Re: [VB6] - Save/Load UDT Arrays Easily
Thanks for pointing that out about the BuildHeader function.
If you change the length of the header, say, to 5, then you will also need to modify the LoadUDT and SaveUDT routines, where it has something like (going from memory here):
Put #intFF, 5, udtArray()
to
Put #intFF, 6, udtArray()
Same for the Get #intFF
The 2nd argument specifies which byte the UDT data starts at, which is immediately after the header. So it should be +1 length of header.
If that doesn't work, you may need to decide on a header length and then delete the file and resave the UDT array.
Re: [VB6] - Save/Load UDT Arrays Easily
Quote:
Originally Posted by DigiRev
Thanks for pointing that out about the BuildHeader function.
No problem.
Quote:
Originally Posted by DigiRev
If you change the length of the header, say, to 5, then you will also need to modify the LoadUDT and SaveUDT routines, where it has something like (going from memory here):
(snip)
If that doesn't work, you may need to decide on a header length and then delete the file and resave the UDT array.
Tried that. Even deleted and resaved. Might not have done it in that order though :P can't think much lately.
will try again. :)
EDIT: works. great job. this simplifies my saving by miles.
Re: [VB6] - Save/Load UDT Arrays Easily
Quote:
Originally Posted by mut80r
No problem.
Tried that. Even deleted and resaved. Might not have done it in that order though :P can't think much lately.
will try again. :)
EDIT: works. great job. this simplifies my saving by miles.
You're welcome, glad it has helped someone. :D This is how I save a lot of data in my programs now also.
Re: [VB6] - Save/Load UDT Arrays Easily
...some posts never grow old!
Thanks for this post DigiRev. I was looking for a simple way to save/retrieve some data to text files in VBA, and this was just the ticket! I massaged your example project into a small class that suits my particular needs, and now I have a great reusable tool.
-Danny