Dim Statement
Use a Dim statement in the Declarations section of a module to declare a variable or a variable array that can be used in all procedures in the module. Use a Dim statement within a procedure to declare a variable used only in that procedure.
Syntax
Dim {[WithEvents] variablename [([<array dimension>],... )] [As [New] datatype]},...
where <array dimension> is
[lowerbound To ] upperbound
Notes
If you do not include an <array dimension> specification but you do include the parentheses, you must include a ReDim statement in eac procedure that uses the array to dynamically allocate the array at run time. You can define an array with as many as 60 dimensions. If you do not include a lowerbound value in an <array dimension> specification, the default lower bound is 0. You can reset the default lower bound to 1 by including an Option Base 1 statement in the module Declarations section. The lowerbound and upperbound values must be integers, and upperbound must be greater than or equal to lowerbound. The number of members of an array is limited only by the amount of memory on your computer.
Valid datatype entries are Byte, Boolean, Integer, Long, Currency, Single, Double, Date, String (for variable-length strings), String * length (for fixed-length strings), Object, Variant, or one of the object types described earlier in this tutorial. You can also declare a userdefined variable structure using the Type statement and then use the user type name as a data type. You should always explicitly declare the data type of your variables. If you do not include the As datatype clause, Visual Basic assigns the Variant data type.
Use the New keyword to indicate that a declared object variable is a new instance of an object that doesn't have to be set before you use it. You can use the New keyword only with object variables to create a new instance of that class of object without requiring a Set statement. You can't use New to declare dependent objects. If you do not use the New keyword, you cannot reference the object or any of its properties or methods until you set the variable to an object using a Set statement.
Use the WithEvents keyword to indicate an object variable within a class module that responds to events triggered by an ActiveX object. Form and report modules that respond to events on the related form and report objects are class modules. You can also define custom class modules to create custom objects. If you use the WithEvents keyword, you cannot use the New keyword.
Visual Basic initializes declared variables at compile time. Numeric variables are initialized to zero (0), variant variables are initialized to empty, variable-length string variables are initialized as zero-length strings, and fixed-length string variables are filled with American National Standards Institute (ANSI) zeros (Chr(0)). If you use a Dim statement within a procedure to declare variables, Visual Basic reinitializes the variables each time you run the procedure.
Examples
To declare a variable named intMyInteger as an integer, enter the following:
Dim intMyInteger As Integer
To declare a variable named dbMyDatabase as a database object, enter the following:
Dim dbMyDatabase As Database
To declare an array named strMyString that contains fixed-length strings that are 20 characters long and contains 50 entries from 51 through 100, enter the following:
Dim strMyString(51 To 100) As String * 20
To declare a database variable, a new table variable, and two new field variables for the table; set up the objects; and append the new table to the Tabledefs collection, enter the following:
Public Sub NewTableExample() Dim db As DAO.Database Dim tdf As New DAO.TableDef, _ fld1 As New DAO.Field, _ fld2 As New DAO.Field ' Initialize the table name tdf.Name = "MyTable" ' Set the name of the first field fld1.Name = "MyField1" ' Set its data type fld1.Type = dbLong ' Append the first field to the Fields ' collection of the table tdf.Fields.Append fld1 ' Set up the second field fld2.Name = "MyField2" fld2.Type = dbText fld2.Size = 20 ' Append the second field to the table tdf.Fields.Append fld2 ' Establish an object on the current database Set db = CurrentDb ' Create a new table by appending tdf to ' the Tabledefs collection of the database db.TableDefs.Append tdf End Sub
To declare an object variable to respond to events in another class module, enter the following:
Option Explicit Dim WithEvents objOtherClass As MyClass Sub LoadClass () Set objOtherClass = New MyClass End Sub Sub objOtherClass_Signal(ByVal strMsg As string) MsgBox "MyClass Signal event sent this " & _ "message: " & strMsg End Sub
In class module MyClass, enter the following:
Option Explicit Public Event Signal(ByVal strMsg As String) Public Sub RaiseSignal(ByVal strText As String) RaiseEvent Signal(strText) End Sub
In any other module, execute the following statement:
MyClass.RaiseSignal "Hello"
In this tutorial:
- Visual Basic Fundamentals
- Visual Basic Development Environment
- Visual Basic Editor Window
- Relationship Between Access and Visual Basic
- Visual Basic Debugging Tools
- Working with the Watch Window
- Variables and Constants
- Variable and Constant Scope
- Declaring Constants and Variables
- Dim Statement
- Enum Statement
- Event Statement
- Private Statement
- Public Statement
- Static Statement
- Type Statement
- Collections, Objects, Properties, and Methods
- DAO Architecture
- ADO Architecture
- Referencing Collections, Objects, and Properties
- Use Exclamation Points and Periods
- Assigning an Object Variable-Set Statement
- Object Methods
- Manipulating Complex Data Types Using DAO
- Working with ADO Recordsets
- Functions and Subroutines
- Sub Statement
- Understanding Class Modules
- Property Let
- Property Set
- Controlling the Flow of Statements
- Do...Loop Statement
- For...Next Statement
- For Each...Next Statement
- If...Then...Else Statement
- RaiseEvent Statement
- Stop Statement
- With...End Statement
- Running Macro Actions and Menu Commands
- Executing an Access Command
- Trapping Errors
- Working with 64-Bit Access Visual Basic for Applications
- Using LongPtr Data Types
- Supporting Older Versions of Access
- Using LongLong Data Types