The ancestor of Visual Basic, the programming lan-
guage Basic, was designed for teaching programming
with a minimum of formalities about the writing. This
meant that there was no need to declare variables at all.
Basic created the necessary variables as they were
needed. This is still possible in Visual Basic, but soft-
ware developers agree that when developing larger
programs it is a huge advantage to declare all variables.
You can tell VBA to insist on declarations. Write this
specification at the beginning of the module
Variant type. Even if you declare a variable, you don't
have to specify its type. Without an explicit type, the
variable is of type Variant. This means that its actual
type may change dynamically according to what the
program stores into it. It may thus hold a number at one
point in time, a text string at another point in time.
Apart from the value in the variable, VBA also stores a
tag telling what the type is at present.
Declarations of variables usually start with the word
Dim (for dimension) followed by a list of the variables.
Figure 6.2A shows such a list
Dim B, C As Byte
The result is that variable B is of type Variant and vari-
able C of type Byte. Unfortunately this is counterintui-
tive and cumbersome. Most people would believe that
B as well as C are of type Byte. You have to specify a
type for each of the variables to avoid them becoming
Simple variables may be of the types shown on the fig-
ure: Byte, Boolean, Integer, etc. We have met most of
them already. Byte, Integer and Long hold integers
with different range. Single and Double hold floating
point numbers with at least 6 significant digits (Single)
or 14 significant digits (Double). (See section 2.2 for
Currency. The Currency type is a funny in-between
intended for keeping track of monetary amounts. It is a
very long integer (64 bits) interpreted as this integer
divided by 10,000. This means that the integer 147,000
is the number 14.7 exactly. Why this rule? It is because
we can guarantee that amounts are rounded correctly as
a bookkeeper would do it. With floating point numbers
you have no control over the round-off.
Date. A date value is technically a Double. The integer
part is the number of days since 12/30-1899 0:00, the
fractional part is the time within the day. As an exam-
ple, the number 1 corresponds to 12/31-1899 at 0:00,
the number 1.75 to 12/31-1899 at 18:00 (6 PM).
Object and Form variables are references to objects,
not the objects themselves. You can set the references
by means of the Set-assignment and test them by
means of the Is-operator.
Variant. You can explicitly declare the variable as
Variant, but if you don't specify a type, the variable is
Variant anyway. Variants can not only hold the simple
values above, but also values such as Null or Empty.
Notice that VBA treats all fields in database records as
Initial values. When a record field is empty, it has the
value Null. When a Variant is just created, it has the
value Empty, because no memory is allocated for the
value. When a String is just created, it holds a text of
length 0. When a numerical variable is just created, it
holds the value 0.
Strings come in two versions. Strings of variable
length and strings of fixed length. The former change
their length to match what the program stores in them.
The length may be very long, nominally 2 billion, in
practice limited by memory space. Strings of fixed
length always use space from the left, and fill up the
remaining characters with spaces (blanks).
The field types text and memo correspond to VBA
strings of variable length.
Arrays can have one or more dimensions. In Figure
6.2A, array c has two dimensions. The first index
ranges from zero to 5 (zero is the default), the second
from 1 to 6. You may specify the type of the elements
of the array. If omitted, they are Variants.
The second array, d, is dynamic, meaning that its di-
mensions and index ranges can change over time. The
program can dynamically change the dimensions and
ranges by means of the Redim statement, but in general
the values stored in the array don't survive this opera-
tion. You can use Redim Preserve if you only change
the range of the last dimension. In that case, the values
You can release the memory occupied by a dynamic
array by means of the Erase statement.
You can declare types of your own (user-defined
types). They will typically take the form of a record
declaration as shown on the figure. Notice that each
field of the record must be specified on a line of its
You can only declare types in simple modules, not in
Form modules. Once you have declared a type, you can
use it for declaring variables and arrays.
There are two kinds of procedures: subroutines and
functions. The only difference is that a function returns
a value. For this reason you can store the result of a
6. Visual Basic reference