I want to create a two dimensional array. I would like to store static values in this array and eventually input load them from a table. I have looked in VB 5 help and have not found anything close to what I would like to do. The help only shows examples of one dimensional arrays with some syntax. Not a good example of what I would like to do. I did find something called the LBound function that showed me how to declare a multiple dimensional array with a range of values . But, I would like these to be static values that I declare.

txtfields(index) as my subscript

Array for coordinates

subscript     x coordinate y coordinate
1                4            15
2                4            38
etc              etc          etc

Also , after creating this array I would like to access it . The help only show examples of accessing either one dimensional arrays or the lbound function which I am thinking would do me no good in this situation since I am declaring the values and they are not in a specific range.

For instance
Dim Array_Test(4,5,6,2)
************Array_test(2) =  5*******************
Who is Participating?
caraf_gConnect With a Mentor Commented:
Well, Green Boy says
"I want to create a two dimensional array"
So I tell him how to create a two dimensional array.

Try again. See my first comment:
First row:
txtFields(1, 1) = 1
txtFields(2, 1) = 4
txtFields(3, 1) = 15

Second Row:
txtFields(1, 2) = 2
txtFields(2, 2) = 4
txtFields(3, 2) = 38

Third Row:
txtFields(1, 3) = 3
txtFields(2, 3) = etc
txtFields(3, 3) = etc

Of course, you don't really need the first element as it simply contains the number.

To have a dynamic number of rows, use Redim.

Dim txtFields() As Long

'code here.... Now you know the number of rows required: N
ReDim txtFields(1 to 2, 1 to N)
'txtFields(1, m) will contain x-coord.
'txtFields(2, m) will contain y-coord.
'm is the row number

And now you can start filling it:
First row:
txtFields(1, 1) = 4
txtFields(2, 1) = 15

Second Row:
txtFields(1, 2) = 4
txtFields(2, 2) = 38

Last Row
txtFields(1, N) = x
txtFields(2, N) = y
"Dim Array_Test(4,5,6,2) "

You just declared a four-dimensional array there.

To get the lbound and ubound of multi-dimensional arrays, you must specify the dimension as well:

Msgbox Ubound(Array_Test, 1) will give you 4

Msgbox Ubound(Array_Test, 2) will give you 5


So you could dim as follows:

Dim txtFields(1 to 3, 1 to 100)

And for the Nth value

txtFields(1, N) would contain the subscript
txtFields(2, N) would contain the x-coordinate
txtFields(3, N) would contain the y-coordinate

that was an answer, methinks
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Green_BoyAuthor Commented:
What about declaring static values not a range of values ? So I guess the comma determines a new dimension when declaring it . Where and how do place static values such as
sub script     x value      y value
1                4            15
2                4            38
etc              etc          etc

Not a range of values
Dim txtFields(1 to 3, 1 to 100)

But something like this ?

Dim txtFields(1 to 3, ?static values?)
Green_BoyAuthor Commented:
Read the text above
When you Dim an array, the numbers  used in the declaration are the upper index bounds, not actual values.


My_Array(9, 9) As Long

is a 2 dimentional array with each dimention of 10 elements (0 to 9).

After dimming it, you can then assign variables to the array.

For i = 0 To 9
    For j = 0 To 9
        My_Array(i, j) = i + j
Green_BoyAuthor Commented:

This is what I have pulled out the help in VB5

This example uses the Array function to return a Variant containing an array.

Dim MyWeek, MyDay
MyWeek = Array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")
' Return values assume lower bound set to 1 (using Option Base
' statement).
MyDay = MyWeek(2)      ' MyDay contains "Tue".
MyDay = MyWeek(4)      ' MyDay contains "Thu".

Maybe I am off the mark . But, all I want to is assign these values in some type of an array. I want to be able to pull both the first and second column using the subscript. This example up above looks to me like a one dimensional array that you can declare the values statically ?

1 column  2nd Column
 1        2
 4        5
 5        10
The Array() function works as stated, but not for more than one dimension.
Also, you are forced to use Variant data.
You cannot hard assign multi-dimensional arrays in VB, it must be done in software.
Use 3 arrays:
   Dim x() As Single, y() As Single
   x = VBA.Array(1, 1, 1, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 4, 4)
   y = VBA.Array(5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 4, 4)
I think you don't know what exactly do you want.

  So think hard, and say again what do you EXACTLY want???
Are you talking about some constant array or something?
If you've done something like that in some other language, write it here so we can help you in Visual Basic!!!
Green_BoyAuthor Commented:
Thanks alot that worked perfect !!!!! Sorry I didn't get back sooner. Your last question comment completed my understanding to your first comment. Thank you very much!!!!
My pleasure. Glad I could help.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.