Your code communicates your meaning by the way in which you use variables. Here are two things you should keep in mind when writing your code:
You should not declare Variant data types.
You should not implicitly declare variables.
These points are discussed in detail in the following subsections.
Variant Data Types
Variant data types were introduced in Hour 2. A Variant data type is a generic data type that incurs a lot of overhead. When a Variant is used, the compiler adds code at runtime to determine the actual data type. Microsoft uses Variant variables in existing code, and Variants are necessary for COM objects. However, it is generally preferable to use the exact data type you need for your variables. This conveys your meaning more precisely and ensures your variables contain the appropriate type and range of values.
COM (or Common Object Model) is a standard for writing compound data types that can be packaged as separate chunks of executable code. Often COM objects are referred to as components.
Implicit Variable Declaration
Access lets you implicitly declare a variable, which means that you simply introduce the variable at the point of use. Listing 3.2 is a revision of Listing 3.1. All the variables in this listing are implicitly defined.
Listing 3.2 Implicit Variable Usage
1: Const PI = 3.14159 2: Radius = 10 3: Circumference = PI * Radius ^ 2
Notice that there are no variable declaration statements. Radius in lines 2 and 3 and Circumference in line 3 are simply introduced at their point of first use. At first glance, this might seem slick, but after a few hundred lines of code, it can cause confusion. You should be aware of this practice but avoid its use.
As a rule, you should write the statement Option Explicit as the first line of code in a module. This prevents you or anyone else from inadvertently introducing implicit variables in your code.
A module is a file that contains Access code._
When you have Option Explicit in your module, Access displays the error message shown in Figure 3.1 if you try to run your code with an implicit variable.
Figure 3.1 Variable not defined error caused by implicit declaration.
It is easy to keep track of implicit variables when you write a few lines of code. However, any useful code has a tendency to grow and evolve over time. If your program contains enough code, it is important to have as many aids to understanding your code as possible. Explicit variable declaration is the preferred way to go.