VB Arrays


Just trying to figure out how to do this with a VB array. I want an array that does the equivalent of this PHP code:

$db_array[1][0] = username
$db_array[1][1] = userid
$db_array[1][2] = email      

Where the first number in the [] would go up from 1 to however number of users I have. The second number in the [] would mean the username [0], userid [1] or email [2] of that first number.

Make sense?

Thanks a lot :-), and let me know if you need clarification. I know I've been pretty vague, but I'm unsure as how I should explain it ;-).
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


First you use:
    Dim db_array() As String

And then redimension it using:
    ReDim Preserver db_array(nuber_of_users, 2) As String

Basically, you can create either static or dynamic arrays. Static arrays must include a fixed number of items, and this number must be known at compile time so that the compiler can set aside the necessary amount of memory. You create a static array using a Dim statement with a constant argument:

' This is a static array.
Dim Names(100) As String

Visual Basic starts indexing the array with 0. Therefore, the preceding array actually holds 101 items.

Most programs don't use static arrays because programmers rarely know at compile time how many items you need and also because static arrays can't be resized during execution. Both these issues are solved by dynamic arrays. You declare and create dynamic arrays in two distinct steps. In general, you declare the array to account for its visibility (for example, at the beginning of a module if you want to make it visible by all the procedures of the module) using a Dim command with an empty pair of brackets. Then you create the array when you actually need it, using a ReDim statement:

' An array defined in a BAS module (with Private scope)
Dim Customers() As String
Sub Main()
    ' Here you create the array.
    ReDim Customer(1000) As String
End Sub

If you're creating an array that's local to a procedure, you can do everything with a single ReDim statement:

Sub PrintReport()
    ' This array is visible only to the procedure.
    ReDim Customers(1000) As String
    ' ...
End Sub

If you don't specify the lower index of an array, Visual Basic assumes it to be 0, unless an Option Base 1 statement is placed at the beginning of the module. My suggestion is this: Never use an Option Base statement because it makes code reuse more difficult. (You can't cut and paste routines without worrying about the current Option Base.) If you want to explicitly use a lower index different from 0, use this syntax instead:

ReDim Customers(1 To 1000) As String

Dynamic arrays can be re-created at will, each time with a different number of items. When you re-create a dynamic array, its contents are reset to 0 (or to an empty string) and you lose the data it contains. If you want to resize an array without losing its contents, use the ReDim Preserve command:

ReDim Preserve Customers(2000) As String

When you're resizing an array, you can't change the number of its dimensions nor the type of the values it contains. Moreover, when you're using ReDim Preserve on a multidimensional array, you can resize only its last dimension:

ReDim Cells(1 To 100, 10) As Integer
ReDim Preserve Cells(1 To 100, 20) As Integer    ' This works.
ReDim Preserve Cells(1 To 200, 20) As Integer    ' This doesn't.

Finally, you can destroy an array using the Erase statement. If the array is dynamic, Visual Basic releases the memory allocated for its elements (and you can't read or write them any longer); if the array is static, its elements are set to 0 or to empty strings.

You can use the LBound and UBound functions to retrieve the lower and upper indices. If the array has two or more dimensions, you need to pass a second argument to these functions to specify the dimension you need:

Print LBound(Cells, 1)   ' Displays 1, lower index of 1st dimension
Print LBound(Cells)      ' Same as above
Print UBound(Cells, 2)   ' Displays 20, upper index of 2nd dimension
' Evaluate total number of elements.
NumEls = (UBound(Cells) _ LBound(Cells) + 1) * _
    (UBound(Cells, 2) _ LBound(Cells, 2) + 1)


Well in his case (dynamic first dimension) is best to use dynamic multi-dimensional array as I wrote in my post above ;)

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.


I think this is what you are looking for:

Dim db_array() as String
Dim NumberOfPeople as Long

NumberOfPeople = 20 'Or however many there are.

Redim db_array(1 to NumberOfPeople, 0 to 2)

db_array(1, 0) = username
db_array(1, 1) = userid
db_array(1, 2) = email


It is better to use a structure instead of a 2d array.  Maybe try this instead.  Put this type in a module called modTypes or something.

Public Type UserType
  Username As String
  UserID as String
  Email as String
End type

Public Type GroupType
  Person() As UserType
  nPeople As Long
End Type

Then you probably have some global variable that you will put the people into.  Maybe add them to another module called modGlobs or something.  Like this:

Public People As GroupType

Then you fill the People type...I'll just hard code a person or two for you like this:

People.nPeople = 2
Redim People.Person(1 To People.nPeople) As UserType

People.Person(1).Username = "Jim"
People.Person(1).UserID = "JJ"
People.Person(1).Email = "jj@aol.com"

People.Person(2).Username = "Nancy"
People.Person(2).UserID = "Nan"
People.Person(2).Email = "nan@aol.com"


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial

You might want to redim from 0 instead of 1.  That depends on what you are doing.  You know something like this:

People.nPeople = 2
Redim People.Person(1 To (People.nPeople-1)) As UserType

People.Person(0).Username = "Jim"
People.Person(0).UserID = "JJ"
People.Person(0).Email = "jj@aol.com"

People.Person(1).Username = "Nancy"
People.Person(1).UserID = "Nan"
People.Person(1).Email = "nan@aol.com"

It really is no different, but some people like to start from 0 instead of 1 (or whatever).

I recommend using types because it makes it easier for you to handle the data as you start to pass it around your program.  Like if you want to deal with one person then you just pass one variable of type UserType.  Instead of having to pass three variables for each field that you associate with the person.  There are a lot of other reasons to be using types instead of splitting your data up into multiple arrays.  Anyway, its just a suggesting.

Opps....I didn't redim that previous message from 0.  It still redim'ing from 1.  Though I did change the upper bounds.  The line:

Redim People.Person(1 To (People.nPeople-1)) As UserType

Should have read:

Redim People.Person(0 To (People.nPeople-1)) As UserType

If you wanted to start at 0 instead of 1.  There are probably other typos since I didn't test anything.  Still, you get the jist of what I am suggesting.
Jimmy_AAuthor Commented:
Whoa, this is what I call service :-). I wake up, and already have this many answers to my question.

I've ended up using the types Enlade detailed (which work perfectly by the way), so have given you the 50 points. I've learnt stuff some all of the other methods, so I've increased the points and split the extra points among the rest of you.

Thanks again! This has been very helpful.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

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.