Constants
Your code may contain unchanging values that appear over and over. Or your code may depend on certain numbers that are difficult to remembernumbers that, in and of themselves, have no obvious meaning.
In these cases you can greatly improve the readability of your codeand make it easier to maintain
by using constants. A constant is a meaningful name that takes the place of a number or string that doesn't change. You can't modify a constant or assign a new value to it as you can to a variable. Constants have one of two sources:
Intrinsic or system-defined constants are provided by applications. Microsoft Access, Visual Basic, and Data Access Objects (DAO) constants are all available in Microsoft Access, along with intrinsic constants from other applications that provide object libraries.
Symbolic or user-defined constants are declared by using the Const statement.
See Also For more information on intrinsic constants, see "Intrinsic Constants" later in this chapter. For more information on user-defined constants, see the following section, "Creating Your Own Constants."
Creating Your Own Constants
You use the Const statement to create your own symbolic constants. The syntax for the Const statement is:
[Public | Private] Const constantname [As type] = expression
The argument constantname is a valid symbolic name (the rules are the same as the rules for creating variable names), and expression is composed of numeric or string constants and operators (you can't use function calls in expression).
A Const statement can represent a mathematical or date/time quantity. For example:
Const conPi = 3.14159265358979
Public Const conMaxPlanets = 9
Const conReleaseDate = #1/1/95#
You can also use the Const statement to define string constants:
Public Const conVersion = "07.10.A"
Const conCodeName = "Enigma"
You can place more than one constant declaration on a single line, if you separate them with commas:
Public Const conPi = 3.14, conMaxPlanets = 9, conWorldPop = 6E+09
The expression on the right side of the equal sign (=) is often a number or literal string, but it can also be an expression that results in a number or string as long as the expression doesn't contain calls to functions. You can even define constants in terms of previously defined constants:
Const conPi2 = conPi * 2
After you define constants, you can place them in your code to make it much more readable, as shown in the following example:
Static SolarSystem(1 To conMaxPlanets)
If lngNumPeople > conWorldPop Then Exit Function
Defining the Scope of User-Defined Constants
A Const statement has scope just as a variable declaration does, and the same rules apply:
To create a constant that's available only within a procedure, declare it within that procedure.
To create a constant that's available to all procedures within a module, but not to any procedure outside that module, declare the constant in the Declarations section of the module.
To create a constant that's available throughout the application, declare the constant in the Declarations section of a standard module and place the Public keyword before Const. Public constants cannot be declared in a form or report module.
Avoiding Circular References
It's important to be aware of one possible complication when you're using public constants: Because constants can be defined in terms of other constants, you must be careful not to set up a circular reference between two or more constants. A circular reference occurs when you have two or more public constants, each defined in terms of the other, as in the following example:
' In Module 1.
Public Const conA = conB * 2
' In Module 2.
Public Const conB = conA / 2
If you create a circular reference, Visual Basic generates an error when it compiles your code. You can't compile your code until you resolve the circular reference. To avoid creating a circular reference, restrict all your public constants to a single module or, at most, to a small number of modules.
Note You can't define a public constant in a form or report module, only in a standard module. You can, however, declare a private constant in a form or report module.
Intrinsic Constants
In addition to the constants you declare with the Const statement, Microsoft Access automatically declares a number of intrinsic constants. These constants are available in all modules.
Because you can't disable intrinsic constants, the constants you create can't have the same names as the intrinsic constants. Also, you can't redeclare or set intrinsic constants to different values.
Important Because the values represented by the intrinsic constants may change in future versions of Microsoft Access, you should use the constants instead of their actual values.
You can use intrinsic constants wherever you can use user-defined constants, including in expressions. The following example shows how you might use the intrinsic constant vbCurrency to determine whether varTest is a Variant of VarType 6 (Currency).
If VarType(varTest) = vbCurrency Then
Debug.Print "varTest contains Currency data."
Else
Debug.Print "varTest does not contain Currency data."
End If
The intrinsic constants for Microsoft Access, as well as those for the Visual Basic for Applications and Data Access Objects (DAO) libraries, are listed in the Object Browser. Other applications that provide object libraries, such as Microsoft Excel and Microsoft Project, also provide a list of constants you can use with their objects, methods, and properties. To see these constants, click the appropriate object library in the Project/Library box of the Object Browser, and then click Constants in the Classes box.
Important In order for constants from other applications to appear in the Object Browser, you must set a reference to the application's object library. Open a module, and then in the References dialog box (Tools menu), select the check box for the object library you want to set a reference for.
See Also For more information on using the Object Browser, see Chapter 5, "Working with Objects and Collections."
Intrinsic constant names are in a mixed-case format, with a two-character prefix indicating the object library that defines the constant. Constants from Microsoft Access are prefaced with "ac"; for example, acDataErrContinue. Constants from the VBA object library are prefaced with "vb" ; for example, vbTileHorizontal. Constants from the DAO object library are prefaced with "db" ; for example, dbRelationUnique. All of these intrinsic constants are available in Microsoft Access without having to be declared.
Note In Microsoft Access versions 2.0 and earlier, constant names appeared in all capital letters with underscoresfor example, A_REFRESH. Microsoft Access 97 supports intrinsic constants from these early versions. To see them, click the Access object library in the Project/Library box of the Object Browser, and then click OldConstants in the Classes box.
See Also For a list of intrinsic constants and more information about them, search the Help index for "intrinsic constants."
© 1996 Microsoft Corporation. All rights reserved.