Part II: Programming Microsoft Access
432
One way to ease the burden of managing the code and objects in an application is through the use
of a naming convention. A naming convention applies a standardized method of supplying names to
the objects and variables in an application.
The most common naming convention used in Access applications uses a three- or four-character
prefix (a tag) attached to the base name of the objects and variables in a VBA application. For
example, a text box containing a person’s last name might be named txtLastName, while a com-
mand button that closes a form would be named cmdClose or cmdCloseForm.
The names for variables follow a similar pattern. The string variable holding a customer name
might be named strCustomer and a Boolean variable indicating whether the customer is cur-
rently active would be either boolActive or fActive (the f indicates a flag value).
Using a naming convention is not difficult. Most of the code in this book uses one- and three-
character prefixes exclusively. In some cases, when the use of the variable is obvious, a one-
character prefix is used (for example, sLastName) to keep code examples short and simple. In
longer procedures, three-character prefixes are used on most variables. Most of the controls on the
Access forms in the projects on this book’s CD use three-character prefixes.
This simple naming convention helps you select the most logical name to apply to the variables
and objects in your applications. In most cases, you’ll assign a name to a variable or object based
on how that item is used in the application. In other words, using a naming convention encourages
names based on the functionality provided by the variables and objects in your applications. After
all, you shouldn’t be adding to an application variables and objects that don’t have specific jobs to
perform.
Understanding variable scope and lifetime
A variable is more than just a simple data repository. Every variable is a dynamic part of the appli-
cation and may be used at different times during the program’s execution. The declaration of a
variable establishes more than just the name and data type of the variable. Depending on the key-
word used to declare the variable and the placement of the variable’s declaration in the program’s
code, the variable may be visible to large portions of the application’s code. Alternatively, a differ-
ent placement may severely limit where the variable can be referenced in the procedures within the
application.
Examining scope
The visibility of a variable or procedure is called its scope. A variable that can be seen and used by
any procedure in the application is said to have public scope. A variable that is usable by a single
procedure is said to have scope that is private to that procedure.
There are many analogies for public and private scope. For example, a company is likely to have a
phone number that is quite public (the main switchboard number) and is listed in the phone book
and on the company’s Web site; each office or room within the company might have its own
extension number that is private within the company. A large office building has a public street