Adding Watch Expressions

Many errors in procedures are caused by variables that assume unexpected values. If a procedure uses a variable whose value changes in various locations, you may want to stop the procedure and check the current value of that variable. Visual Basic offers a special Watch window that allows you to keep an eye on variables or expressions while your procedure is running. To add a watch expression to your procedure, select the variable whose value you want to monitor in the Code window, then choose Debug | Add Watch. The screen will display the Add Watch dialog box, as shown in Figure 9-9.

Figure 9-9: The Add Watch dialog box allows you to define conditions you want to monitor while a VBA procedure is running.

The Add Watch dialog box contains three sections, which are described in Table 9-2.

Part I

Table 9-2: Add Watch dialog sections




Displays the name of a variable you have highlighted in your procedure. If you opened the Add Watch dialog box without selecting a variable name, type the name of the variable you want to monitor in the Expression text box.


In this section, indicate the name of the procedure that contains the variable and the name of the module where this procedure is located.

Watch Type

Specifies how to monitor the variable. If you choose:

• the Watch Expression option button, you can read the value of the variable in the Watch window while in break mode.

• Break When Value Is True, Visual Basic will automatically stop the procedure when the variable evaluates to true (nonzero).

• Break When Value Changes, Visual Basic will automatically stop the procedure each time the value of the variable or expression changes.

You can add a watch expression before running a procedure or after suspending the execution of your procedure.

The difference between a breakpoint and a watch expression is that the breakpoint always stops a procedure in a specified location, but the watch stops the procedure only when the specified condition (Break When Value Is True or Break When Value Changes) is met. Watches are extremely useful when you are not sure where the variable is being changed. Instead of stepping through many lines of code to find the location where the variable assumes the specified value, you can put a watch breakpoint on the variable and run your procedure as normal. Let's see how this works.

©Hands-On 9-1: Working with the Watch Window

1. Insert a new standard module and name it Breaks.

2. Prepare the following WhatDate procedure as shown in Figure 9-10:

Sub WhatDate()

Dim curDate As Date Dim newDate As Date Dim x As Integer curDate = Date For x = 1 To 365

The WhatDate procedure uses the For.. .Next loop to calculate the date that is x days in the future. You won't see any result when you run this procedure unless you insert the following instruction in the procedure code just before the End Sub keywords:

MsgBox "In " & x & " days, it will be " & NewDate

This time, however, you don't want to display the individual dates, day after day. Suppose that you want to stop the program when the value of the variable x reaches 211. In other words, you want to know what date will be

Introduction to Access 2003 VBA Programming

211 days from now. To get the answer, you could insert the following statement into your procedure before the Next x statement:

If x = 211 Then MsgBox "In " & x & " days it will be " & NewDate

But this time, you want to get the answer without introducing any new statements into your procedure. If you add watch expressions to the procedure, Visual Basic will stop the For.. .Next loop when the specified condition is met, and you'll be able to check the values of the desired variables.

Figure 9-10: Using the Watch window.

3. Choose Debug | Add Watch.

4. In the Expression text box, enter the following expression: x = 211.

5. In the Context section, choose WhatDate from the Procedure combo box and Breaks from the Module combo box.

6. In the Watch Type section, select the Break When Value Is True option button.

7. Click OK to close the Add Watch dialog box. You have now added your first watch expression.

8. In the Code window, position the insertion point anywhere within the name of the curDate variable.

9. Choose Debug | Add Watch and click OK to set up the default watch type with the Watch Expression option.

10. In the Code window, position the insertion point anywhere within the name of the newDate variable.

11. Choose Debug | Add Watch and click OK to set up the default watch type with the Watch Expression option.

Part I

After performing the above steps, the WhatDate procedure contains the following three watches:

12. Position the cursor anywhere inside the code of the WhatDate procedure and press F5.

Visual Basic stops the procedure when x = 211 (see Figure 9-10 above). Notice that the value of the variable x in the Watches window is the same as the value you specified in the Add Watch dialog. In addition, the Watches window shows the value of the variables curDate and newDate. The procedure is in break mode. You can press F5 to continue, or you can ask another question: What date will be in 277 days? The next step shows how to do this.

13. Choose Debug | Edit Watch and enter the following expression: x = 277.

14. Display the Edit Watch dialog box by double-clicking the expression in the Watch window.

15. Click OK to close the Edit Watch dialog box. Notice that the Watches window now displays a new value of the expression. The x is now false.

16. Press F5. The procedure stops again when the value of x = 277. The value of curDate is the same. However, the newDate variable now contains a new value — a date that is 277 days from now. You can change the value of the expression again or finish the procedure.

17. Press F5 to finish the procedure without stopping.

When your procedure is running and a watch expression has a value, the Watches window displays the value of the watch expression. If you open the Watches window after the procedure has finished, you will see the error "<out of context>" instead of the variable values. In other words, when the watch expression is out of context, it does not have a value.

Removing Watch Expressions

To remove a watch expression, click on the expression you want to remove from the Watches window and press Delete. Remove all the watch expressions you defined in the preceding exercise.

If you want to check the value of an expression not defined in the Watch window, you can use Quick Watch (see Figure 9-11).

To access the Quick Watch dialog box while in break mode, position the insertion point anywhere inside a variable name or an expression you want to watch and choose Debug | Quick Watch, or press Shift+F9.

x = 211 curDate newDate

Break When Value Is True Watch Expression Watch Expression

0 0

Post a comment