See Also Example
Declares symbolic constants for use in place of values.
[Global] Const constantname = expression [, constantname = expression] . . .
The Const statement has these parts:
Global Reserved word that can precede Const in modules to declare constants that can be referred to by all procedures in all forms and modules.
constantname Name of the constant.
expression Expression assigned to the constant. It can consist of literals (such as 1.0), other constants, or any of the arithmetic or logical operators except exponentiation (^). You also can use a single string literal, such as "Error on input". You cannot use string concatenation, variables, user-defined functions, or intrinsic Visual Basic functions (such as Chr[$]) in expressions assigned to constants.
Tip Constants can make your programs self-documenting and easier to modify. Unlike variables, constants can't be inadvertently changed while your program is running.
You can add a type-declaration character For example: to constantname to indicate the data type of the constant, but this character is not part of the name.
Const MAXDIM% = 250
If you don't use a type-declaration character in the name, the constant is given a data type based on the expression in the Const statement. Strings always yield a String constant, but numeric expressions are evaluated and the constant is given the simplest type that can represent the constant. Because a constant must be a single predefined type, it can't be a Variant . By definition, a Variant can appear as different data types depending on context. Constants are not affected by Deftype statements, such as DefInt.
Caution Constants must be defined before referring to them. Put Global constant definitions in a single module to avoid a cross-module dependency that cannot be resolved.
Constants declared in a Sub A constant declared outside a procedure (in the Declarations section of a module) is defined throughout the module in which it is declared. Constants declared outside a procedure using the Global reserved word can be used by all procedures in all forms and modules. You can use constants anywhere you would use an expression. or a Function procedure are local to that procedure.
Note You cannot use the Global keyword to declare constants in forms, because form constants are available only to procedures within the form where they are declared.
Tip Use all uppercase letters for constant names to make them easy to recognize in your program listings.
Const Statement Example
The example uses Const to define the symbolic constant PI. To try this example, paste the code into the Declarations section of a form. Then press F5 and click the form.
Sub Form_Click ()
Const PI = 3.141592654 ' Define constant.
Dim Area, Circum, Msg, Radius ' Declare variables.
Msg = "Enter the radius of a circle in centimeters."
Radius = InputBox(Msg) ' Get user input.
Circum = 2 * PI * Radius ' Calculate circumference.
Area = PI * (Radius ^ 2) ' Calculate area.
Msg = "The circumference of the circle is "
Msg = Msg & Circum & " cm. Its area is "
Msg = Msg & Area & " cm."
MsgBox Msg ' Display message.