Declaring Constants


VB supports the use of symbolic, or named constants. Constants are similar to variables, except that you provide a value for the constant when you declare it, and its value can never change. The syntax for declaring a constant is:


[Public | Global | Private] Const constantname [As datatype] = expression


A global (project-level) constant can only be declared in a standard (.bas) module (not a form), using "Public Const" or "Global Const" (the "Public" keyword is preferred). Module-level constants can be declared in the General Declarations Section of either a standard or form code module using "Private Const" (or just "Const"; the default is "Private"). Local-level constants are declared in any procedure of a standard or form module just using the word "Const" (no "Public" or "Private"). If you omit the "As datatype" clause, VB will use its "best guess" as to what the datatype should be, based on the expression.


The following table shows how constants may be declared and the location of their declaration affects the scope:



Keyword Used to Declare the



Where Declared


General Declarations Section of a Form (.frm) Module


General Declarations Section of a Standard (.bas) Module


Sub or Function procedure of a Form or Standard Module


module-level scope

module-level scope

local-level scope


Private Const

module-level scope

module-level scope

not allowed


Public Const


Global Const


not allowed


project-level scope


not allowed



Sample Constant Declarations:


Public Const gsngTAX_RATE As Single = 0.06


Private Const mdtmCUT_OFF_DATE As Date = #1/1/1980#


Const strERROR_MESSAGE = "Invalid Data" 'String data type assumed


Note that in Const declarations, string literals are delimited with double quotes ("), date literals are delimited with pound signs (#), and numeric literals are not delimited.


Naming Conventions for Constants


Similar to naming variables, use the lowercase three-character datatype prefix ("int", "str", etc.), prefixed by an "m" if a module-level constant or "g" if a global (project-level) constant. However, for the main part of the name, use all capital letters (with underscores to break up individual words within the name).