Declares constants for use in place of literal values.
[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 Byte, 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 that includes all arithmetic or logical operators except Is. |
Constants are private by default. Within procedures, constants are always private; their visibility cant be changed. In standard modules, the default visibility of module-level constants can be changed using the Public keyword. In class modules, however, constants can only be private and their visibility cant be changed using the Public keyword.
You can combine several constant declarations on the same line by separating each constant assignment with a comma. If constant declarations are combined in this way, the Public or Private keyword, if used, applies to all of them.
You cant use 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 easy to modify. Unlike variables, constants cant be inadvertently changed while your program is running.
If you dont explicitly declare the constant type (using As type), the constant is given the 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 can use an expression.
#Const Directive, Deftype Statements, Function Statement, Let Statement, Property Get Statement, Property Let Statement, Property Set Statement, Sub Statement.
A constant in a Microsoft Access standard module is private by default. If you precede a constant with the keyword Public, however, it is then available to other modules in the current database. It is also available to modules that reference the current database. A constant defined in a form module or report module is always private to that module, and cant be made public using the Public keyword.
This example uses the Const statement to declare constants for use in place of literal values. Public constants occur in a the General section of a standard module, rather than a Class Module. Private constants can go in the General section of any type of module.
' Constants are Private by default.MyVar = 459 ' Declare Public constant. Const MyString = "HELP" ' Declare Private Integer constant.Const MyInt As Integer = 5 ' Declare multiple constants on same line.MyStr = "Hello", MyDouble As Double = 3.4567
In Microsoft Access, you can declare only private constants in the Declarations section of a form module, report module, or standard module. However, you can declare both public and private constants in the General section of a standard module.
' In form or report module.conCommission = .08 ' In standard module.conInterest = .054Const conAddress = "8421 58th Avenue, College Park, MD"