Microsoft Office Tutorials and References
In Depth Information
Chapter 3: Writing Smarter Code
The quick-and-dirty way is to simply make up a variable name and assign
a value by following the name with an equal sign (=) and the value to be
stored in the variable. The following VBA statements define three variables
named x, y, and ExtPrice. The variable x stores the number 10; the variable
y stores the number 9.99; and the variable ExtPrice stores the result of
multiplying the contents of x by the contents of y (or 99.9, by the time all
three lines are executed):
x = 10
y = 9.99
ExtPrice = x * y
All these statements are examples of implicit variable declarations — which
basically means that you make up variables as you go.
Explicit variable declarations, as the name implies, require you to assign a
data type to each variable before you assign values to variables. Explicit
variable declaration is a little more work than implicit variable declaration,
but your code runs more smoothly and efficiently because Access doesn’t
have to figure out the best data type to use when it encounters the data
lurking in the variable.
Two steps go into using a variable explicitly:
1. You define (or declare) the variable, which gives the variable a data type.
2. After the variable exists, you assign a value, using the same syntax as for
implicit declarations: variableName = value .
The command for defining a variable explicitly is Dim, which is short for
dimension. Thinking of Dim as standing for define in memory may be easier,
however, because variables exist only in the computer’s random access
memory (RAM). The simplified syntax for the Dim statement looks like this:
Dim varname [As type ] [,...]
varname is a name of your own choosing, and type refers to one of the
acceptable VBA data types or object types. The data types that you assign
to variables in a Dim statement are the same as those that you use to define
arguments in a Function or Sub statement.
The comma and ellipsis in the syntax chart mean that you can define
multiple variables, separated by commas, within a single statement. The following
example statement declares one variable, named ReportName, as a string
(textual data):
Dim ReportName As String
 
Search JabSto ::




Custom Search