Declaring an Array Variable

 < Day Day Up > 

Because an array is a type of variable, you must declare it and specify a data type, as you must any other variable. When declaring an array, specify the array's upper and lower bounds. In other words, let the array know the smallest and largest values it can store.

To declare an array variable, use the following syntax:

 

 Dim arrayvariable([lower to ]upper) [AS datatype] 

where arrayvariable is the variable's name, lower is the array's smallest index, upper is the array's largest index, and datatype defines the type of data the array can store. If you omit the datatype argument, VBA declares the array as a Variant array.

In its simplest form, you can omit lower and VBA will assume it's 0. We recommend that you specify both bound arguments unless you have a specific reason to delay their definition.

Now, let's look at a simple example of an array declaration statement:

 

 Dim arrInteger(1 To 4) As Integer 

Right away you know a few things about this array:

  • arrInteger can contain up to four elements. Think of each element as a "slot" within the array where you can store a value.

  • arrInteger can store only Integer values.

  • The elements (slots) in arrInteger are numbered 1, 2, 3, and 4.

     < Day Day Up > 


    Automating Microsoft Access with VBA
    Automating Microsoft Access with VBA
    ISBN: 0789732440
    EAN: 2147483647
    Year: 2003
    Pages: 186

    Similar book on Amazon

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net