#Const Directive

Description

Used to define conditional compiler constants for Visual Basic.

Syntax

#Const constname = expressionThe #Const compiler directive syntax has these parts:

Part Description
   
constname Name of the constant follows standard variable naming conventions.
expression Literal, other conditional compiler constant, or any combination that includes 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 (a constant defined with Const) or using a constant that is undefined causes an error to occur. Conversely, constants defined using the #Const keyword can’t be used for anything but 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 Module 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 Declarations 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 = 0 : conDebug = 1

Using a logical operator, you can include both constants in an #If...Then...#Else construct. In the following example, you can run the first segment of code only if an expression containing both constants is true.


#If (conActiveLanguage = 0 And conDebug = 1) Then
    .        ' Run debug code for the active language version.
    .
    .
#Else
    .        ' Run another code segment.
    .
    .
#End If

Note 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. In the Code View section, 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.