Const Statement

Description

Declares constants for use in place of literal values.

Syntax

[Public | Private] Const constname [As type] = expression

The Const statement syntax has these parts:

Part

Description

Public

Used at module level to declare constants that are available to all procedures in all modules. Not allowed in procedures.

Private

Used at module level to declare constants that are available only within the module where the declaration is made. Not allowed in procedures.

constname

Name of the constant; follows standard variable naming conventions.

type

Data type of the constant; may be Boolean, Integer, Long, Currency, Single, Double, Date, String, or Variant. Use a separate As type clause for each constant being declared.

expression

Literal, other constant, or any combination including arithmetic or logical operators except Is.


Remarks

If not explicitly specified using either Public or Private, constants are Private by default.

Several constant declarations can be combined on the same line by separating each constant assignment with a comma. If constant declarations are combined in this way, the Public or Private keywords, if used, apply to all of them.

You can't use string concatenation, variables, user-defined or intrinsic functions (such as Chr) in expressions assigned to constants.

If you don't explicitly declare the constant type (using As type), the constant is given a data type that is most appropriate for the expression provided.

Constants declared in Sub, Function, or Property procedures are local to that procedure. A constant declared outside a procedure is defined throughout the module in which it is declared. You can use constants anywhere you would use an expression.

See Also

Deftype Statements, Let Statement.

Example

This example uses the Const statement to declare constants for use in place of literal values.


' Constants are Private by default.
Const MyVar = 459

' Declare Public constant.
Public Const MyString = "HELP"

' Declare Private Integer constant.
Private Const MyInt As Integer = 5

' Declare multiple constants on same line.
Const MyStr = "Hello", MyDouble As Double = 3.4567