Comparing VBA Programming Constructs (VBA Programming Concepts) (AutoCAD VBA)

This section describes what is meant by each of the terms macro, procedure, function, program, and application, including the relationships and differences that exist among them.

Macros

A macro is a set of instructions that is executed each time its name or associated shortcut key is used. Macros can be one-liners or they can be quite lengthy and complex— they can even call other macros and run applications. Macros cannot have parameters, and they cannot return values, but they can be run from the AutoCAD window.

Macros are declared in the same way as normal procedures, using the Sub statement. For example:

tmp757e193_thumb

If you include any parameters inside the parentheses, for example,

tmp757e194_thumb


Visual Basic will treat this block of code as a standard procedure, and it will not appear in the list in the Macros dialog box that’s displayed from the AutoCAD window.

Procedures

A procedure contains a set of coding statements that perform a specific task. Visual Basic procedures are a series of statements enclosed between Sub and End Sub statements.

Procedures enable you to split a lengthy piece of code up into smaller more manageable parts, with each part becoming a procedure. It is good programming practice to use procedures when the same task needs to be performed from several different places—to avoid duplicate code.

For example, the Shelf Specification application shown in Figure 4.13 contains a procedure that examines the key pressed by the user to enter data into a text box, checking to see if it is a numeric character. If not, the application cancels the keystroke so that the non-numeric character is not appended to the string in the text box. The procedure is called by the KeyPress event procedures of all three text boxes.

Shelf Specification UserForm

Figure 4.13 Shelf Specification UserForm

Exercise 4.2: The Shelf Specification Application for Dimensioning Shelves

The following steps show you how to develop the Shelf Specification application:

1.    Start a new project and add a UserForm with three labels, three text boxes, and a command button.

2.    Set the properties as shown in Table 4.1.

3.    Type the CheckNumericKey procedure shown in Listing 4.2 into the General Declarations section of the UserForm. Enter the calls to it into the skeleton Keypress event procedures of the text boxes.

Table 4.1 Control Properties for Shelf Specification Application

Object

Caption

UserForm1

Shelf Specification

Labell

Height

Label2

Width

Label3

Thickness

Command Button

Continue

4. Run your application from the Visual Basic Editor window and try entering some letters and numbers. The letters will be cancelled by the CheckNumeric-Key procedure and won’t appear in any of the text boxes. Only when you type numeric characters will the numbers they represent be appended to the text box strings.

Listing 4.2: CheckNumericKey Procedure

Listing 4.2: CheckNumericKey Procedure

Analysis

•    Line 1 starts the CheckNumericKey procedure, which has one parameter that expects to be passed the character entered by the user. This procedure cancels any character entered that is outside the range 0 to 9.

•    Line 2 verifies that the Keyascii character entered by the user is not in the range 48 through 57. The ASCII code for 0 is 48, and the ASCII code for 9 is 57, so the condition is True if a non-numeric key has been pressed.

•    Line 3 runs when a non-numeric key has been pressed; it sets the Keyascii variable to zero. When zero is passed back to the calling event procedure, the user input is cancelled.

•    Line 4 ends the If statement.

•    Line 5 ends the CheckNumericKey procedure.

•    Line 6 is a blank line to make the code easier to read.

•    Line 7 starts the KeyPress event procedure of TextBoxl. The KeyPress event procedure has one parameter that is passed the ASCII code of the key pressed by the user while entering data into TextBoxl.

•    Line 8 calls the CheckNumericKey procedure with Keyascii as the argument. If a numeric key has been entered, the Keyascii value returned is appended to the end of the string displayed in the text box. If a non-numeric key has been entered,the Keyascii value returned is zero,and nothing is appended to the string.

•    Line 9 ends the KeyPress event procedure of TextBoxl.

•    Line 10 is a blank line to make the code easier to read.

•    Lines 11-13 and 15-17 contain the Keypress event procedures of the other two text boxes, which both call the CheckNumericKey procedure in exactly the same way as the KeyPress event procedure of TextBox1.

•    Line 14 is a blank line to make the code easier to read.

Next post:

Previous post: