Declares variables and allocates storage space.
Dim varname[([subscripts])] [As [New] type] [, varname[([subscripts])] [As [New] type]] . . . |
The Dim statement syntax has these parts:
Part | Description |
varname | Required. Name of the variable; follows standard variable naming conventions. |
subscripts | Optional. Dimensions of an array
variable; up to 10 multiple dimensions may be declared. The subscripts argument
uses the following syntax: count1[, count2] . . . where count1, count2 are constants, indicating the upper limit of allowable indices for the defined array. The lower limit of allowable indices always equals 0. So, for a one-dimensional array the number of elements can be calculated as count1+1 . |
New | Optional. Keyword that enables implicit creation of an object. If you use New when declaring the object variable, a new instance of the object is created during declaration, so you don't have to use the Set statement to assign the object reference. The New keyword can't be used to declare variables of any intrinsic data type, can't be used to declare instances of dependent objects or objects that don't have built-in constructor. |
type | Optional. Data type of the variable; may be Byte, Boolean, Integer, Long, Single, Double, Date, String (for variable-length strings), String * length (for fixed-length strings), Object, Variant, or an object type. Use a separate As type clause for each variable you declare. |
Variables declared with Dim at the module
level are available to all procedures within the module.
At the procedure level, variables
are available only within the procedure.
Use the Dim statement at module or procedure level to declare the data
type of a variable. For example, the following statement declares a variable
as an Integer.
Dim Number As Integer
Also use a Dim statement to declare the object type of a variable. The
following declares a variable for a new instance of a database engine.
Dim Eng As New dbEngine
If the New keyword is not used when declaring an object variable, the
variable that refers to the object must be assigned an existing object using
the Set statement before it can be used. Until it is assigned to an object,
the declared object variable has the special value Nothing, which indicates
that it doesn't refer to any particular instance of an object. When you use
the New keyword in the declaration, an instance of the object will be
created.
You can also use the Dim statement with empty parentheses to declare
a dynamic array. After declaring a dynamic array, use the ReDim statement
within a procedure to define the number of dimensions and elements in the array.
If you don't specify a data type or object type, the variable is Variant
by default.
All declared variables except those declared with New, take the Empty
value, which indicates that they are not initialized.
Tip: It's recommended to place all declarations
in the beginning of a module or a procedure. This shortens the time of compilation.
This example shows the Dim statement used to declare variables. It also shows the Dim statement used to declare arrays.
' AnyValue and MyValue are declared as Variant by default. Dim AnyValue, MyValue ' Explicitly declare a variable of type Integer. Dim Number As Integer ' Multiple declarations on a single line. AnotherVar is of type Variant ' because its type is omitted. Dim AnotherVar, Choice As Boolean, BirthDate As Date ' DayArray is an array of Variants with 51 elements indexed, from ' 0 thru 50 Dim DayArray(50) ' Matrix is a two-dimensional array of integers. Dim Matrix(3, 4) As Integer ' MyArray is a dynamic array of variants. Dim MyArray() |
See Also |
Data Type Summary, ReDim Statement, Set Statement, Static Statement, Const Statement |