Results 1 to 6 of 6

Thread: [VB6] - Save/Load UDT Arrays Easily

  1. #1

    Thread Starter
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    [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.
    Attached Images Attached Images  
    Attached Files Attached Files

  2. #2
    Junior Member
    Join Date
    Dec 2007
    Posts
    28

    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.

  3. #3

    Thread Starter
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    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.

  4. #4
    Junior Member
    Join Date
    Dec 2007
    Posts
    28

    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.
    Last edited by mut80r; Mar 21st, 2008 at 06:08 PM. Reason: Fixed It

  5. #5

    Thread Starter
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    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. This is how I save a lot of data in my programs now also.

  6. #6
    Lively Member 5applerush's Avatar
    Join Date
    Nov 2006
    Location
    Shelby, Mi
    Posts
    98

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width