All About Constants (VBA Programming Concepts) (AutoCAD VBA)

A constant is the term given to a value that remains unchanged as long as the application is running. The values of some constants (such as pi) may never change. Others (such as TaxRates) may change from time to time. Such values should all be declared as constants and given names that can be used throughout the code. The names of constants follow the same naming conventions as variable names.

Replacing a constant value by a name not only makes the code easier to read and understand, but also easier to maintain. Any change to a constant’s value need only be updated once, where the constant is declared and assigned its value. This saves you the time of looking for instances of a value that you want to change—and the errors possible in this process, because you could easily mistype the new value, miss an instance of the original value altogether, or update a value that has nothing to do with the constant.

Constants come in two flavors:

Symbolic Constants Also known as user-defined constants, symbolic constants are the ones you create with symbolic names and assigned values; they are declared in your code using the Const statement.

Intrinsic Constants Also known as built-in constants, intrinsic constants are an integral part of VBA and its controls for use with objects, methods, and properties.

Declaring Symbolic Constants

Symbolic (or user-defined) constants are declared in a Const statement. The definition of a constant resembles an equation starting with its name, followed by an equals sign, and then its value. The value can be a number, a string, an expression, or another constant.

For example:


You can also specify a constant’s type in the Const statement. For example:


If you don’t specify the type, VBA uses the one that is the closest match to the constant’s value.

Several constants can be declared on the same line separated by commas:


Constants with different types can also be declared on the same line, like this:


If you have a lot of constants to declare, sort them into small logical groups and declare them on consecutive lines, with a few on each line.

Viewing Built-in Constants in the Object Browser

VBA provides lots of built-in (intrinsic) constants that have been predeclared and assigned values ready for you to use without having to declare them. You can see these constants in the Object Browser window (shown in Figure 4.2).

Object Browser displaying the constants belonging to the enumerated type acColor

Figure 4.2 Object Browser displaying the constants belonging to the enumerated type acColor

All of the libraries listed in the Library box have their own set of constants, qualified by two-letter prefixes that identify the library they belong to. For example, constants from the AutoCAD library are prefixed with the letters ac, those from the MSForms library are prefixed fm, and those from the VBA library with vb. This enables you to install applications that have VBA capability and know that you won’t get heaps of errors because of duplicate declarations of constants. Without this helpful prefix (if constants had only a name appropriate to their represented value), it would be impossible for you to confidently name your own constants without duplicating existing ones. In addition, there could be future problems when you update to new versions of software, should they duplicate one of your names.

The constants you yourself define are automatically placed in the ACADProject library and will not be listed with a prefix unless you declare them as such.

The following steps show you how to access the intrinsic constants listed in the Object Browser:

1.    Open the Visual Basic Editor, and choose Insert ^ Userform to add a UserForm to your project.

2.   If the Object Browser window isn’t already on display, choose View ^ Object Browser.

3. Click the down-arrow button to open the Project/Library list box. A dropdown list of libraries appears, as shown here:


4.    Select <All Libraries> from the list. The <All Libraries> item appears in the Project/Library box.

5.    Scroll down the Classes list and select fmBorders. Information about the fmBorders item appears in the Details pane at the bottom of the Object Browser, and its members appear in the Members Of list, as shown in Figure 4.3. The fmBorders item is an Enumerated type,which means it can only be assigned one value from a set of predefined values, using either its name or its value.

Constant fmBordersLeft selected from the Members of fmBorders list

Figure 4.3 Constant fmBordersLeft selected from the Members of fmBorders list

6.    Click the fmBordersLeft item from the Members Of list. The Details pane now contains information about this constant, including its value of 2 (see Figure 4.3).

7.    One by one, click the other members of fmBorders. Notice how the values assigned to each member lie in the range 0 through 3; this sequential nature of values is also a characteristic of all Enumerated types.

Using the Object Browser’s Search Engine

The Object Browser window offers a search facility to help you find strings within a library. You enter the string into the Search Text box directly under the Project/Library box. You can enter any string you like and even use VBA wildcards (see the accompanying sidebar).

The drop-down list from the Search text box contains up to five of the last strings you searched for since opening the IDE. When you click the Search button (the binoculars icon), the Object Browser searches the library for every occurrence of the string entered. You can also specify that you want to search for the whole word only, by right-clicking the Search button and selecting Find Whole Word Only from the shortcut menu, as shown here.


When the search is complete, the list of found items appears in the Search Results pane. It lists the library, class, and member names for each found item.


Constants vs. Variables for Efficiency: If a value will never change throughout the run time of the code, it is best to declare the value as a constant. This will make your code more efficient so it will run faster. When translated, any constant name is replaced by its actual value. That means during run time all the values are already in place; in contrast, the value of a variable has to be retrieved from memory each time its name is encountered.

VBA Wildcard Characters

You can include wildcard characters in your search string in the Object Browser. When it comes across these characters during the search,VBA tries to replace the wildcard with a character or a string of characters in an attempt to maximize the    number of items it can find.These wildcard characters are available:

?    To replace any single character

*    To replace any number of characters

#    To replace any single digit

Next post:

Previous post: