Monitoring Procedure Values

   

Many runtime and logic errors are the result of (or, in some cases, can result in) variables or properties assuming unexpected values. If your procedure uses or changes these elements in several places, you'll need to enter break mode and monitor the values of these elements to see where things go awry. The Visual Basic Editor offers a number of methods for monitoring values, and I discuss them in the next few sections.

Using the Locals Window

Most of the values you'll want to monitor will be variables. Although watch expressions (discussed in the next section) are best if you want to keep an eye on only one or two variables, the Visual Basic Editor gives you an easy method to use if you want to monitor all the variables in any procedure. This method makes use of a special Visual Basic Editor window called the Locals window. You can display this window by activating the View, Locals Window command.

When your procedure enters break mode, the Locals window displays a line for each declared variable in the current procedure. As you can see in Figure 15.7, each line shows the variable name , its current value, and its type.

Figure 15.7. Use the Locals window to keep track of the current value of all your variables.

graphics/15fig07.jpg

Adding a Watch Expression

Besides monitoring variable values, VBA also lets you monitor the results of any expression or the current value of an object property. To do this, you need to set up a watch expression that defines what you want to monitor. These watch expressions appear in the Watch window, which you can display by activating the View, Watch Window command.

To add a watch expression, follow these steps:

  1. If the expression exists inside the procedure (for example, an object property), select the expression as follows :

    • For single-word expressions, place the insertion point anywhere inside the word.

    • For more complex expressions, highlight the entire expression.

  2. Select Debug, Add Watch to display the Add Watch dialog box, shown in Figure 15.8. (Note that in the vast majority of cases you can skip steps 35, below, and head right to step 6.)

    Figure 15.8. Use the Add Watch dialog box to add watch expressions.

    graphics/15fig08.jpg

  3. If the expression you want to monitor isn't already shown in the Expression text box, enter the expression. You can enter a variable name, a property, a user -defined function name, or any other valid VBA expression.

  4. Use the Context group to specify the context of the variable (that is, where the variable is used). You enter the Procedure and the Module.

  5. Use the Watch Type group to specify how VBA watches the expression:

    • Watch Expression Displays the expression in the Watch window when you enter break mode.

    • Break When Value Is True Tells VBA to automatically enter break mode when the expression value becomes True (or nonzero).

    • Break When Value Changes Automatically enters break mode whenever the value of the expression changes.

  6. Click OK.

Once you've added a watch expression, you monitor it by entering break mode and examining the expression in the Watch window, as shown in Figure 15.9.

Figure 15.9. The Watch window with a few watch expressions.

graphics/15fig09.jpg

Editing a Watch Expression

You can make changes to a watch expression while in break mode. Follow these steps:

  1. Select the Watch window by clicking it or by selecting View, Watch Window.

  2. Click the watch expression you want to edit.

    tip

    graphics/tip_icon.gif

    The Debug, Add Watch command is available when you're in break mode, so you can add more watch expressions if necessary.

  3. Select Debug, Edit Watch (or press Ctrl+W; you can also double-click the watch expression). VBA displays the Edit Watch dialog box.

  4. Make your changes to the watch expression.

  5. Click OK to return to the Debug window.

Deleting a Watch Expression

To delete a watch expression you no longer need to monitor, follow these steps:

  1. Select the Watch window by clicking it or by selecting View, Watch Window.

  2. Click the watch expression you want to edit.

  3. Select Debug, Edit Watch to display the Edit Watch dialog box.

  4. Click the Delete button (or highlight the expression in the Watch window and press the Delete key). VBA deletes the expression and returns you to the Debug window.

Displaying Data Values Quickly

Many variables, properties, and expressions are set once, and they don't change for the rest of the procedure. To avoid cluttering the Watch window with these expressions, VBA offers a couple of methods for quickly displaying an expression's current value: Data Tips and Quick Watch.

The Data Tips feature is one of the handiest of VBA's debugging tools. When you're in break mode, simply move the mouse pointer over the variable or property you want to know the value of. After a brief pause, VBA displays a banner showing the current value of the expression, as shown in Figure 15.10.

Figure 15.10. VBA displays Data Tips when you hover the mouse pointer over an expression in break mode.

graphics/15fig10.jpg

The Quick Watch feature displays a dialog box that shows the expression, its current context, and its current value. To try this, follow these steps:

  1. Enter break mode.

  2. Either place the insertion point inside the expression you want to display or highlight the expression.

  3. Select Debug, Quick Watch (or press Shift+F9). The Visual Basic Editor displays a Quick Watch dialog box like the one shown in Figure 15.11.

    Figure 15.11. Use the Quick Watch dialog box to quickly display the value of an expression.

    graphics/15fig11.jpg

If you want to add the expression to the Watch window, click the Add button. To return to break mode without adding the expression, click Cancel.



Absolute Beginner's Guide to VBA
Absolute Beginners Guide to VBA
ISBN: 0789730766
EAN: 2147483647
Year: 2003
Pages: 146

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net