Dim Statement

Description

Declares variables and allocates storage space.

Syntax

Dim varname[([subscripts])][As [New] type][, varname[([subscripts])][As [New] åtype]] . . .

The Dim statement syntax has these parts:

Part Description
varname Name of the variable; follows standard variable naming conventions.
subscripts Dimensions of an array variable; up to 60 multiple dimensions may be declared. The subscripts argument uses the following syntax:

[lower To] upper [,[lower To] upper] . . .

When not explicitly stated in lower, the lower bound of an array is controlled by the Option Base statement. The lower bound is zero if no Option Base statement is present.
New Keyword used to indicate that a declared object variable is a new instance of a Visual Basic object or an externally creatable OLE Automation object. The New keyword can’t be used to declare variables of any intrinsic data type and can’t be used to declare instances of dependent OLE Automation objects.
type Data type of the variable; may be Byte, Boolean, Integer, Long, Currency, Single, Double, Date, String (for variable-length strings), String * length (for fixed-length strings), Object, Variant, a user-defined type, or an object type. Use a separate As type clause for each variable you declare.


Remarks

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 NumberOfEmployees 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 worksheet.


Dim X As New Worksheet

If the New keyword is not used when declaring an object variable, no instance of the object actually exists. A variable that refers to an object must be assigned an existing object using the Set statement before it can be used. Until it is assigned an object, the declared object variable has the special value Nothing, which indicates that it does not refer to any particular instance of an object.

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 try to redeclare a dimension for an array variable whose size was explicitly specified in a Private, Public, or Dim statement, an error occurs.

If you do not specify a data type or object type, and there is no Deftype statement in the module, the variable is Variant by default.

When variables are initialized, a numeric variable is initialized to 0, a variable-length string is initialized to a zero-length string, and a fixed-length string is filled with zeros. Variant variables are initialized to Empty. Each element of a user-defined type variable is initialized as if it were a separate variable.

Tip When you use the Dim statement in a procedure, it is a generally accepted programming practice to put the Dim statement at the beginning of the procedure.

See Also

Array Function, Option Base Statement, Private Statement, Public Statement, ReDim Statement, Set Statement, Static Statement, Type Statement.

Specifics (Microsoft Access)

You can use the New keyword to declare an object variable of a specific type. If you include the New keyword in your variable declaration, you automatically create a new instance of the object and point the object variable to it. Therefore, if you declare an object variable using the New keyword, you don’t need to use the Set statement.

Using the New keyword, you can create an object variable to point to any type of object — a Microsoft Access object, data access object, user-defined object, or Visual Basic Collection object. For example, you can use the following statements to create a new TableDef object.


' Create object variable and point it to new object.tdfOrders As New TableDef
' Set Name property of object..Name = "Orders"

These statements are equivalent to the following two statements.


' Create an empty object variable.tdfOrders As TableDef
' Create the new TableDef object and point the object variable to it.
' "dbs" is an object variable pointing to the current database.tdfOrders = dbs.CreateTableDef("Orders")

You should use the New keyword to create a Collection object. For example, the following statement creates a new Collection object.


Dim colCustomers As New Collection

You should also use the New keyword to create a new instance of a user-defined object. For example, a form whose module contains public procedures can be a user-defined object. If a form called CustomerObject has a module in which you have defined procedures as public, you can create a new instance of this form object from another module, and its procedures become methods and properties of the object. You can then manipulate the form object by calling the methods and setting the properties you defined in the form module.


' Create new instance of your object.frmCustomer As New Form_CustomerObject
' Call the method Show from the form object..Show

You can use the New keyword to create a new instance of a Microsoft Access object from some applications that are OLE Automation controllers. Check the other application’s documentation to determine whether or not the application supports this syntax.

For example, from another OLE Automation controller, assuming you have established a reference to the Microsoft Access type library, you can create a new Microsoft Access object with the following code.


Dim appAccess As New Access.Application

Example

This example shows various uses of the Dim statement to declare variables. It also shows the Dim statement being used to declare arrays. The default lower bound for array subscripts is 0 and can be overridden at the module level using the Option Base statement.


' AnyValue and MyValue are declared as Variant by default with values
' set to Empty.AnyValue, MyValue
' Explicitly declare a variable of type Integer.Number As Integer
' Multiple declarations on a single line. AnotherVar is of type Variant
' because its type is omitted.AnotherVar, Choice As Boolean, BirthDate As Date
' DayArray is an array of Variants with 51 elements indexed, from
' 0 thru 50, assuming Option Base is set to 0 (default) for
' the current module.DayArray(50)
' Matrix is a two-dimensional array of integers.Matrix(3, 4) As Integer
' MyMatrix is a three-dimensional array of doubles with explicit
' bounds.MyMatrix(1 To 5,  4 To 9,  3 To 5) As Double
' BirthDay is an array of dates with indexes from 1 to 10.BirthDay(1 To 10) As Date        
' MyArray is a dynamic array of variants.MyArray()