PDA

View Full Version : Solved: How to properly redimension an array



jamescol
07-11-2004, 08:07 PM
Say I start out with any array of unknown size:

[vCode]
Option Base 1
Dim myArray() as Variant
[/Code]

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

Jacob Hilderbrand
07-11-2004, 08:38 PM
Dim myArray() As Variant


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:



Redim myArray(1 To x)


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



Redim Preserve myArray(1 To x + 10)

jamescol
07-11-2004, 08:49 PM
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

Jacob Hilderbrand
07-11-2004, 09:02 PM
Glad to help.

Take Care

Scottie P
07-11-2004, 09:13 PM
Word coders scare me! "Ooooo..."