This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.
Constants use multiple-word descriptive names in all uppercase letters with an underscore character between each word. Constants are declared by using the Const statement along with the name of the constant, its data type, and its value. For example, the following constant could be declared in the Declarations section of a module to provide the path to the data source used by an application:
Public Const DATABASE_PATH As String = "C:\Solutions\Source\AppData.mdb"
Note By using the Public keyword to declare the constant, that constant can be used by any procedure in any module in the application. If the Public keyword is not used, the constant has a module-level scope, meaning that it is available only to procedures within the module in which it was declared. If the constant is declared within a procedure, it is available only to the code in the procedure and only as long as the code in the procedure is executing.
Here are some examples of constant names that use the general naming guidelines described earlier:
Note If you create public enumerated constants in a class module, you can use a different naming convention to distinguish them from other constants.
In addition to the constants you declare yourself, Microsoft® Visual Basic® for Applications (VBA), Visual Basic Scripting Edition (VBScript), and each of the Microsoft® Office applications contain built-in, or intrinsic, constants whose values are predefined. Intrinsic constants always should be used in place of the values they represent. As with user-defined constants, the advantage to using intrinsic constants is that they make your code more understandable. For example, compare the following two code samples, where one sample uses intrinsic constants and the other does not. See if you agree that intrinsic constants can make a big difference in how easy the code is to understand.
If MsgBox("Proceed Now?", 48 + 512 + 3 + 16384, "Continue?") = 7 Then DoCmd.OpenForm "Customers", 0, , , 1, 3 End If If MsgBox("Proceed Now?", vbExclamation + vbDefaultButton3 + _ vbYesNoCancel + vbMsgBoxHelpButton, "Continue?") = vbNo Then DoCmd.OpenForm "Customers", acNormal, , , acFormEdit, acDialog End If
For a complete listing of intrinsic constants available through VBA and each of the Office applications, open the Object Browser, select the appropriate type library from the Projects/Library dialog box, type the appropriate constant prefix in the Search text box, and then click Search on the Object Browser toolbar. The following table is a sampling of the complete listing of intrinsic constants.
|Application/type library||Constant prefix|