Const Directive

Named Arguments


#Const constantname = expression constantname Use: Required

Data Type: Variant (String)

Name of the constant.


Use: Required if a

#Const Directive 113

Data Type: Literal

Any combination of literal values, other conditional compilation constants defined with the #Const directive, and arithmetic or logical operators except Is.


Defines a conditional compiler constant. By using compiler constants to create code blocks that are included in the compiled application only when a particular condition is met, you can create more than one version of the application using the same source code. This is a two-step process:

• Defining the conditional compiler constant. This step is optional; conditional compiler constants that aren't explicitly defined by the #Const directive but that are referenced in code default to a value of 0 or False.

• Evaluating the constant in the conditional compiler #If...Then statement block.

A conditional compiler constant can be assigned any string, numeric, or logical value returned by an expression. However, the expression itself can consist only of literals, operators other than Is, and another conditional compiler constant.

When the constant is evaluated, the code within the conditional compiler #If...Then block is compiled as part of the application only when the conditional compiler constant evaluates to True.

You may wonder why you should bother having code that is compiled only when a certain condition is met, when a simple If...Then statement could do the same job. The reasons are:

• You may have code that contains early bound references to objects that are present only in a particular version of the application. You'd want that code compiled only when you know it wouldn't create an error.

• You may wish to include code that executes only during the debugging phase of the application. It's often wise to leave this code in the application even after the application has been released, so that you can check back over a procedure if an issue arises. However, you don't want the code to be executed in the final application. The answer is to wrap your debugging code in a conditional statement. You can then provide a conditional constant that acts as a switch to turn debugging code on or off, as the example below demonstrates.

• Although most operations performed with conditional compilation can be replicated with normal If...Then code blocks, conditional compilation reduces the size of the compiled application and thereby the amount of memory required for the application, making for a more efficient application.

Rules at a Glance

• Conditional compiler constants are evaluated by the conditional compiler #If...Then statement block.

• You can use any arithmetic or logical operator in the expression except Is.

114 Chapter 7- The Language Reference

• You can't use other constants defined with the standard Const statement in the expression.

• According to the documentation, you can't use intrinsic functions in the expression; e.g., #Const MY_CONST = Chr(13) is illegal. In most cases, VBA displays a "Compile error : Variable not found" message if you try this. But there are numerous exceptions. For example, the use of the Int function in the following code fragment doesn't produce a compiler error, and in fact, successfully defines a constant ccDefInt whose value is 3:

#Const ccDefFloat = 3.1417 #Const ccDefInt = Int(ccDefFloat)

• When using #Const, you can't use variables to assign the conditional constant a value.

• Constants defined with #Const can be used only in conditional code blocks.

• Constants defined with #Const have scope only within the module in which they are defined; i.e., they are private.

• You can place the #Const directive anywhere within a module.

• You can't use the #Const directive to define the same constant more than once within a module. Attempting to do so produces a "Compile Error: Duplicate Definition" error message.

• Interestingly, you can define the same constant both through the VB or VBA interface (see the second item in the "Programming Tips & Gotchas" section) and using the #Const directive. In this case, the constant defined through the interface is visible throughout the application, except in the routine in which the #Const directive is used, where the private constant is visible.

• The #Const directive must be the first statement on a line of code. It can be followed only by a comment. Note that the colon, which combines two complete sets of statements onto a single line, can't be used on lines that contain #Const.


#Const ccDebug = 1 'evaluates to true

Function testValue(sValue as String)

sValue = UCase(sValue) testValue = sValue

#If ccDebug Then

'this code only executes if ccDebug evaluates to true Debug.Print sValue #End If

End Function

Programming Tips & Gotchas

• Conditional compiler constants help you debug your code, as well as provide a way to create more than one version of your application. You can include

#Const Directive 115

code that operates only when run in debug mode. The code can be left in your final version and won't compile unless running in the debugger. Therefore, you don't need to keep adding and removing debugging code.

• You can also define conditional constants outside of the application's code. In the VBA Editor, enter the conditional compiler constant into the Conditional Compilation Arguments text box on the General tab of the Project Properties dialog. You can reach it by selecting the Project Properties option (where Project is the name that you've assigned to the project) from the Tools menu. In Visual Basic, the Conditional Compilation Arguments text box is found on the Make property sheet of the Project Properties dialog. It can be accessed by selecting the Project Properties option (again, where Project is the name that you've assigned to the project) from the Project menu. In Access, the Conditional Compilation Arguments text box is found on the Advanced property sheet of the Options dialog, which can be accessed by selecting the Options item from the Tools menu. Conditional compiler constants defined in this way are public to the project.

Was this article helpful?

0 -1

Post a comment