Previous Page

Using Conditional Compilation

You use conditional compilation to specify the parts of your program you want Microsoft Access to include or ignore when compiling and running. Using conditional compilation, you can maintain a single version of your application that behaves differently under certain conditions. For example, you may use conditional compilation to:

 Include specific features of your program in different versions of your application. For example, you may want to design your application to run on different platforms.

 Change the date and currency display filters for an application distributed in several different languages.

 Include or exclude code used for debugging your application.

Structuring Code for Conditional Compilation

Visual Basic provides special statements called directives for creating conditional compilation constructs. You use the #Const directive to declare a Boolean conditional compilation constant. You then evaluate this constant within the #If...Then...#Else directive.

To conditionally compile a portion of your code, enclose it between #If...Then and #End If statements, using the conditional compilation constant as the branching test. When you want this segment of code to be compiled and run, set the value of the constant to True (-1). Otherwise, set the constant to False (0).

For example, suppose you want to include a portion of code only in an administrator's copy of your application. Start by wrapping this segment in an If...Then statement preceded by a number sign (#).

#If Admn Then
   .
   . ' Insert code to be compiled and run only for an administrator's
   . ' version.
#End If

If the value of the constant Admn is set to True at compile time, Visual Basic compiles and runs the conditional code. Otherwise, Visual Basic ignores it.

Note   Unlike regular Visual Basic code, you can't include other statements on the same line as a conditional compilation statement by separating those statements with colons.

Declaring Conditional Compilation Constants

You can declare conditional compilation constants by setting the Conditional Compilation Arguments option on the Advanced tab of the Options dialog box (Tools menu). The list should contain simple assignment statements separated by colons. For example:

Admn = True : Ansi = 0

Alternatively, you can explicitly declare conditional compilation constants in the Declarations section of the module containing the #If...Then and #Else statements, as follows:

#Const Admn = True

Conditional compilation constants have a special scope and cannot be accessed from standard code. While constants declared with the #Const statement are private to the module in which they are declared, constants declared in the Options dialog box are public to all modules in your application.

Only conditional compilation constants and literals can be used in expressions that you specify by way of the user interface or with the #Const statement. Any undeclared identifier used in a conditional compilation expression generates a compile error.

Using Conditional Compilation for Debugging

You can use conditional compilation to remove debugging statements from the application you distribute to users. To do this, use conditional compilation to include these statements during development, and then ignore these statements in the version of your application that you distribute to users.

The following example procedure uses these statements to display an assertion message when a function is passed a value it isn't designed to handle. You may use a function like this one while writing and debugging your code. Once you've finished debugging your code and you're ready to distribute your application to users, you no longer need the function.

Sub Assertion(blnExpr As Boolean, strMsg As String)
   If Not blnExpr Then
      MsgBox strMsg, , "Assertion"
   End If
End Sub
Sub AProcedure(intX As Integer)
   #If fDebug Then
      Assertion intX < 10000 and intX > 0, "Argument out of range"
   #End If
   ' The code can now assume the correct value.
End Sub

Because the call to the Assertion procedure is conditionally compiled, it is only run if fDebug is set to True. When you compile your application to distribute it to users, set fDebug to False. As a result, the application will compile and run as efficiently as possible.

© 1996 Microsoft Corporation. All rights reserved.

Next Page


Casa de Bender