Chapter 11: Mastering VBA Data Types and Procedures
423
Although you can make up almost any name for a variable, most programmers adopt a standard
convention for naming variables. Some common practices include the following:
l (^) Using a mix of uppercase and lowercase characters, as in TotalCost.
l Using all lowercase characters, as in counter.
l (^) Separating the parts of a variable’s name with underscores, as in Total_Cost.
l Preceding the name with the data type of the value. A variable that stores a number
might be called intCounter, while a variable holding a string might be named
strLastName.
Note
One source of endless confusion to Access developers is the fact that Access object names (tables, queries,
forms, and so on) may contain spaces, while variable names never include spaces. One reason not to use
spaces in Access object names is to eliminate confusion when mixing different naming conventions within a
single application. You’re really better off being consistent in how you apply names to your Access objects,
variables, procedures, and other application entities.
Tip
When creating variables, you can use uppercase, lowercase, or both to specify the variable or call it later. VBA
variables are not case-sensitive. This fact means that you can use the TodayIs variable later without having to
worry about the case that you used for the name when you created it; TODAYIS, todayis, and tOdAyIs all
refer to the same variable. VBA automatically changes any explicitly declared variables to the case that was
used in the declaration statement (the Dim statement).
When you need to see or use the contents of a variable, you simply reference its name. When you
specify the variable’s name, the computer program goes into memory, finds the variable, and gets
its contents for you. This process means, of course, that you need to be able to remember and cor-
rectly reference the name of the variable.
Declaring variables
There are two principle ways to add variables to your applications. The first method — called
implicit declaration — is to let VBA automatically create the variables for you. As with most things
that are not carefully controlled, you’ll find that letting VBA prepare your variables for you is not a
particularly good idea and can lead to performance issues or efficiency in your programs (see the
“Comparing implicit and explicit variables” section, later in this chapter).
Implicit declaration means that VBA automatically creates a variant-type variable for each iden-
tifier it recognizes as a variable in an application. (Variants are discussed in the “Working with
Data Types” section, later in this chapter.) In the following, there are two implicitly declared vari-
ables — strFirstName and strLastName In this example, two string variables (strFirst-
Name and strLastName) are assigned the text contained in two text boxes (txtFirstName and
txtLastName), and a third string variable (strFullName) is assigned the combination of
strFirstName and strLastName, with a space between them.