Microsoft Office 2000/Visual Basic Programmer's Guide |
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, it 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 so long as the code in the procedure is executing.
Here are some examples of constant names that use the general naming guidelines described earlier:
ACCESS_CONNECTSTRING
API_MAX_STRINGBUFFER
SQL_STRING
Note If you create public enumerated constants in a class module, you can use a different naming convention to distinguish them from other constants. For more information about naming enumerated constants in class modules, see Chapter 9, "Custom Classes and Objects."
In addition to constants you declare yourself, Visual Basic for Applications (VBA), Microsoft Visual Basic Scripting Edition (VBScript), and each of the Office applications contain built-in, or intrinsic, constants whose values are predefined. Intrinsic constants should always be used in place of the values they represent. Just like 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 box, type the appropriate constant prefix in the Search Text box, and then click Search on the Object Browser toolbar.
Application/type library | Constant prefix |
Access | ac |
Excel | xl |
FrontPage | fp |
Office | mso |
OfficeBinder | bind |
Outlook | ol |
PowerPoint | pp |
Word | wd |
VBA | vb |
For a complete listing of intrinsic constants available through VBScript, see the VBScript Language Reference in the C:\Program Files\Microsoft Visual Studio\Common\IDE\IDE98\MSE\1033\Vbscrip5.chm file.
Note The path to the Vbscript5.chm Help file reflects the language ID folder (1033) for U.S. English language support in Office. The language ID folder below C:\Program Files\Microsoft Visual Studio\Common\IDE\IDE98\MSE differs for each language.