Debug Menu Commands (Debugging Your Code) (AutoCAD VBA)

The aim of the game is to find out where your application went wrong and why. Because bugs can be introduced to an application in a variety of ways, there are no steadfast, tried-and-tested techniques that you can apply when hunting down the bug. VBA’s debugging features are nonetheless a big help.

The Visual Basic Editor’s Debug menu commands (Figure 10.3) enable the code to be compiled into p-code and checked for syntax errors. Other commands allow you to step through your code statement-by-statement, or to stop execution temporarily at a designated statement (breakpoint). You can even watch the value of variables change as you execute each statement.

Debug menu commands

Figure 10.3 Debug menu commands

Using Breakpoints

When your application doesn’t quite perform up to scratch, you’ll more than likely be able to narrow the problem down to a few statements or a single procedure. In the process of debugging, you can set a breakpoint at the start of the problem area, so that when you run your application it will temporarily stop at the breakpoint with all the variable and property values still intact.


The following steps show you how to set a breakpoint at the problematical If statement from Listing 10.1:

1.    Display the txtTelephone_KeyPress event procedure in the Code window.

2.    In the Margin Indicator bar that runs down the left border of the Code window, click next to the If statement. The line is highlighted, and a dot appears in the Margin Indicator bar.

tmp89fa-227_thumb

3. Run the Input Validation application you set up earlier, and enter the letter a in the text box when prompted. The Code Window appears with the breakpoint statement highlighted and an arrow in the Margin Indicator bar.

tmp89fa-228_thumb

4. Pause the mouse cursor over any of the variables, and a ToolTip box appears with their values.

When you’ve stopped at a breakpoint, you can drag the little yellow arrow forward or backward to skip or repeat code in the same procedure.

Using Quick Watch

The Quick Watch command displays the values of highlighted sections of code. This is useful for evaluating whole expressions. Let’s continue with the If statement in the Input Validation example:

1. Highlight the If condition as shown in Figure 10.4, and choose Debug + Quick Watch.

As you can see in the figure, the Quick Watch dialog box appears, with the Context frame indicating the project, the UserForm, and event procedure. The Expression frame contains the segment you have highlighted, and the Value frame contains the result of evaluating the expression. (Of course, you need to have an idea of the value you are expecting in order to know whether the result is correct or not.)

 Using the Quick Watch feature to evaluate the condition in the If statement

Figure 10.4 Using the Quick Watch feature to evaluate the condition in the If statement

2. Click the Add button in the Quick Watch window.

You’ll see the Watches window (Figure 10.5), containing the information from the Quick Watch dialog box. This window will remain open and allow you to continue watching the selected expression as you work. (The Watches window is described more thoroughly in the upcoming section “Using the IDE’s Debugging Windows.”)

Watches window

Figure 10.5 Watches window

In the Quick Watch window, the value of a variable or a property is shown in a ToolTip if you pause the mouse over that element. An expression can be evaluated in much the same way by highlighting it before positioning the mouse.

Stepping Through Code

When execution has stopped at a breakpoint, there are four commands available in the Debug menu to help you step through the code. Each command works in a slightly different way.

Step Into This command allows you to step through the code statement by statement. It puts your application into run mode, executes the current statement, moves to the next statement, and reverts to break mode. The next statement can be in another procedure.

Step Over With this command you can step over the procedure or function called by the current statement. The procedure or function is completely executed “en block” before breaking at the next statement, which is the one after the statement making the call.

Step Out This command steps out of the current procedure by executing any remaining statements, returning to the calling procedure, and breaking at the statement after the call.

Run to Cursor This command runs all the statements, from the current one all the way to the statement containing the insertion cursor, and then enters break mode again.

If you want to watch the value of a variable, property, or expression as it changes over a block of code rather than at a particular breakpoint, you will benefit from making use of the debugging windows that are described in the next section.

Next post:

Previous post: