Using Conditional Compilation

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

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 condition. When you want this segment of code to be compiled and run, set the value of the conditional compilation 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 use a colon to include other statements on the same line as a conditional compilation statement.

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 avoid compiling 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 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 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
Sub Assertion(blnExpr As Boolean, strMsg As String)
	If Not blnExpr Then
		MsgBox strMsg, , "Assertion"
	End If
End Sub

Because the code in the Aprocedure procedure is conditionally compiled, it is only run if the fDebug variable is set to True. When you compile your application to distribute it to users, set fDebug to False. As a result, the application will not compile or run your debugging code.