Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


VB Arrays

Posted on 2004-08-22
Medium Priority
Last Modified: 2012-06-27

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 ;-).
Question by:Jimmy_A
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 22

Assisted Solution

DarkoLord earned 136 total points
ID: 11863183

First you use:
    Dim db_array() As String

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


Assisted Solution

hamood earned 132 total points
ID: 11863271
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)


LVL 22

Expert Comment

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 18

Assisted Solution

JR2003 earned 132 total points
ID: 11863518

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


Accepted Solution

Enlade earned 200 total points
ID: 11864295

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 = ""

People.Person(2).Username = "Nancy"
People.Person(2).UserID = "Nan"
People.Person(2).Email = ""


Expert Comment

ID: 11864307

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 = ""

People.Person(1).Username = "Nancy"
People.Person(1).UserID = "Nan"
People.Person(1).Email = ""

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

Expert Comment

ID: 11864319

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.

Expert Comment

ID: 11864339

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.

Author Comment

ID: 11865595
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.

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article ( for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question