Microsoft Office Tutorials and References
In Depth Information
Chapter 3: Writing Smarter Code
If you want all your arrays to start at 1 rather than 0, another alternative is
to simply put the following statement in the Declarations section of the
module, before the first procedure in the module:
Option Base 1
After you add the Option Base 1 statement to the top of a module, all
arrays within that module start at 1 rather than 0. Thus, the Dim Colors(3)
statement creates an array of three elements numbered 1, 2, and 3, as you
expect. There is no Colors(0) when the optional base for arrays is set to 1
via the Option Base 1 module declaration.
Working with multidimensional arrays
A multidimensional array offers more than one subscript per name. The
simplest example is a two-dimensional array, which you can envision as a table.
The first subscript in a two-dimensional array represents the element’s row
position in the array, and the second subscript represents the element’s
column position in the array. In the following example array, State(3,2)
refers to row 3, column 2 in the States array, which contains “AZ”:
State(1,1) = “Alabama”
State(1,2) = “AL”
State(2,1) = “Alaska”
State(2,2) = “AK”
State(3,1) = “Arizona”
State(3,2) = “AZ”
State(50,1) = “Wyoming”
State(50,2) = “WY”
The Dim statement that creates a two-dimensional array named States,
with 50 row elements and 2 column elements, is shown with the following
statement. If you use the Option Base 1 statement in the Declarations
section, the starting number for each array is 1:
Dim States (50,2) as String
The code that populates the array (that is, assigns a value to each variable)
looks like this:
Book VIII
Chapter 3
State(1,1) = “Alabama”
State(1,2) = “AL”
State(2,1) = “Alaska”
State(2,2) = “AK”
State(3,1) = “Arizona”
State(3,2) = “AZ”
...
State(50,1) = “Wyoming”
State(50,2) = “WY”
Although many programming languages support multidimensional arrays,
you won’t use them in Access very often. Instead, you can use a table to
store lists and tables of data and then use Access code to extract data from
that table as needed.
Search JabSto ::




Custom Search