Part II: Programming Microsoft Access
536
continued
The #Const directive you see in the preceding figure can appear anywhere in the module as long as
it’s placed above the #If directive. The logical place for the #Const is in the module’s declaration
section, since #Const values are global to the module. In the figure, the compiler constant is set to
True, which means the statements between #If and #End If will be compiled into the application’s
VBA project. In this case, the MsgBox statement is processed and appears in the user interface.
Removing the #Const directive (perhaps by commenting it out) or setting its value to False suppresses
the MsgBox statement.
Compiler directives also can be used for statements other than MsgBox. You could, for example, use
compiler directives to conditionally compile features, additional help, or other capabilities into an
application. Compiler directives are particularly effective for suppressing MsgBox statements that are
used for debugging purposes and must be squelched before giving the application to users. You can
easily reactivate MsgBox statements by setting the #Const statement to True.
Perhaps the biggest impediment to using compiler constants is that the #Const statement is module-
level in scope. A compiler constant declared in one module is not seen by other modules in the appli-
cation. This means that you must add compiler constants to every module in which you want to employ
conditional compilation.
Access provides the Conditional Compilation Arguments text box in the General tab of the applica-
tion’s Project Properties dialog box (Tools ➪ Application Name Properties) to get around this constraint.
As shown in the following figure, you use the Conditional Compilation Arguments text box to specify
any number of compiler constants that apply to the entire application. These settings make it very easy
to toggle conditional compilation from a single location in the application, instead of changing the
#Const statements in every module.
The Project Properties dialog box provides a convenient way to set conditional compilation arguments
for the entire application.