#Const Directive
Description
Used to define conditional compiler constants for Visual Basic.
Syntax
#Const constname = expression
The #Const compiler directive syntax has these parts
Part | Description |
|
constname | Required; Variant (String). Name of the constant; follows standard variable naming conventions. |
expression | Required. Literal, other conditional compiler constant, or any combination that includes any or all arithmetic or logical operators except Is. |
Remarks
Conditional compiler constants are always Private to the module in which they appear. It is not possible to create Public compiler constants using the #Const directive. Public compiler constants can only be created in the user interface.
Only conditional compiler constants and literals can be used in expression. Using a standard constant defined with Const, or using a constant that is undefined, causes an error to occur. Conversely, constants defined using the #Const keyword can only be used for conditional compilation.
Conditional compiler constants are always evaluated at the module level, regardless of their placement in code.
See Also
#If...Then...#Else directive, Const statement.
Specifics (Microsoft Access)
In Microsoft Access, you can define a public conditional compiler constant in the Declarations section of a module. A public compiler constant is applicable to all modules in the current database, but not in any other database.
You can also declare a public compiler constant by clicking Options on the Tools menu, then clicking the Advanced tab. Enter the constant in the Conditional Compilation Arguments box.
For example, you might enter the following expression into the Conditional Compilation Arguments box.
conDebug = 1
You can use a public compiler constant in code in any module. For example, you can create an #If...Then...#Else construct to optionally run debug code.
#If conDebug = 1 Then
. ' Run debug code.
.
.
#Else
. ' Run streamlined code.
.
.
#End If
To create multiple public conditional compilation constants, declare them on separate lines in the Declaration section. In the Conditional Compilation Arguments box, separate them with colons. For example, you can enter the following conditional compilation constants in the Conditional Compilation Arguments box.
conActiveLanguage = 1 : conDebug = 1
You can use a logical operator to include both constants in an #If...Then...#Else construct. In the following example, the first segment of code runs only if both constants are currently equal to 1, so that the expression containing the constants is true.
#If (conActiveLanguage = 1 And conDebug = 1) Then
. ' Run debug code for the active language version.
.
.
#Else
. ' Run another code segment.
.
.
#End If
If either or both constants have a different value, then the code in the #Else portion of the construct runs. To change which block of code runs, you can simply change the values of the constants.
Notes
- A conditional compilation constant is always evaluated with a text-based string comparison method. This evaluation is equivalent to having an Option Compare Text statement in the module and occurs even if the module contains an Option Compare Database statement.
- When writing code for conditional compilation, it may be less confusing to view one procedure at a time rather than all procedures in the module. To change how you view your code, click Options on the Tools menu, then click the Module tab. Under Code View, clear the Full Module View check box.
Example
This example uses the #Const directive to declare conditional compiler constants for use in #If...#Else...#End If constructs.
#Const DebugVersion = 1 ' Will evaluate true in #If block.