Consulting

Results 1 to 5 of 5

Thread: Solved: How to properly redimension an array

  1. #1
    VBAX Tutor jamescol's Avatar
    Joined
    May 2004
    Location
    Charlotte, NC
    Posts
    251
    Location

    Solved: How to properly redimension an array

    Say I start out with any array of unknown size:

    [vba]
    Option Base 1

    Dim myArray() as Variant
    [/vba]

    During the procedure, I query a table for the number of rows it contains. Now I need to allocate enough space in the array to hold 1 element for each row. How do I properly accomplish this task?

    Next, I hit another table, and need to allocate more space in the array for its rows, while maintaining the data the array already contains? How do I do that?

    Thanks,
    James
    "All that's necessary for evil to triumph is for good men to do nothing."

  2. #2
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    [vba]

    Dim myArray() As Variant
    [/vba]

    Is fine to start, but you would be better off not using variant if you don't have to.

    Lets say you determine that there are x elements needed then:

    [vba]
    Redim myArray(1 To x)
    [/vba]

    Now, you want to add 10 to the upperbound, but keep the data from 1 to x.

    [vba]
    Redim Preserve myArray(1 To x + 10)
    [/vba]

  3. #3
    VBAX Tutor jamescol's Avatar
    Joined
    May 2004
    Location
    Charlotte, NC
    Posts
    251
    Location
    Thanks Jacob - works great! I did dim the array as string. What I was trying before forced me to use variant.

    You guys make this look too easy!

    Cheers,
    James
    "All that's necessary for evil to triumph is for good men to do nothing."

  4. #4
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Glad to help.

    Take Care

  5. #5
    Just A Dude VBAX Tutor Scottie P's Avatar
    Joined
    May 2004
    Location
    Remote from 18901 USA
    Posts
    263
    Location
    Word coders scare me! "Ooooo..."
    Life is Visual: Presence is Perception...
    How we see the world is how we respond to it. ~* Peace *~

Posting Permissions

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