O'Reilly logo

VB & VBA in a Nutshell: The Language by Paul Lomax

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

#Const Directive

Named Arguments

No

Syntax

#Const constantname
						=
						expression

constantname

Use: Required

Data Type: Variant (String)

Name of the constant.

expression

Use: Required

Data Type: Literal

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

Description

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 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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required