Working with Watch Expressions
Sometimes it is not enough to use the Immediate window to test the value of an expression or variable. You might want to keep a constant eye on the expression's value. Access 95 introduced the capability to set watches before running a procedure or while code execution is suspended. After you add a watch expression, it appears in the Watch window. As you'll see, you can create several types of watches.
Using Auto Data Tips
The quickest and easiest way to view the value contained within a variable is to use Auto Data Tips, which is an option for working with modules. This feature is available only when your code is in Break mode. While in Break mode, simply move your mouse over the variable or expression whose value you want to check. A tip appears with the current value. To set the Auto Data Tips option from the VBE, choose Tools, Options, click the Editor tab, and check the option for Auto Data Tips, which is under the Code Settings options.
Using a Quick Watch
A quick watch is the most basic type of watch. To add a quick watch, highlight the name of the variable or expression you want to watch and click the Quick Watch button on the toolbar. The Quick Watch dialog box, shown in Figure 15.13, appears. You can click Add to add the expression as a permanent watch or choose Cancel to view the current value without adding it as a watch. If you click Add, the Watches window appears, like the one in Figure 15.14. The next section discusses this window in more detail.
Figure 15.13. The Quick Watch dialog box enables you to quickly view the value of a variable or add an expression as a permanent watch.

Figure 15.14. You can add a watch expression in the Watches window.

Adding a Watch Expression
As you saw, you can add a watch expression using a quick watch. Adding a watch this way does not give you full control over the nature of the watch, however. If you need more control over the watch, you must choose Debug, Add Watch. The Add Watch dialog box appears, as shown in Figure 15.15.
Figure 15.15. The Add Watch dialog box enables you to easily designate all the specifics of a watch expression.

TIPIf you add a quick watch, or you add a watch by choosing Debug, Add Watch, you easily can customize the specifics of the watch by clicking with the right mouse button over the watch in the Watches window. Then select Edit Watch.A quick way to add a watch to the Watches window is to click and drag a variable or expression from a Code module into the Watches window. Access adds the watch with default settings.In the Expression text box, enter a variable, property, function call, or any other valid expression. It is important to select the procedure and module in which you want to watch the expression. Next, indicate whether you want to simply watch the value of the expression in the Immediate window, break when the expression becomes True, or break whenever the value of the expression changes. The sections that follow cover the two latter options.
Editing a Watch Expression
After you add a watch, you might want to edit the nature of the watch or remove it entirely. You use the Edit Watch dialog box to edit or delete a watch expression. Follow these steps:
Figure 15.16. You can use the Edit Watch dialog box to modify the specifics of a watch after you add it.

Breaking When an Expression Is True
A powerful aspect of a watch expression is that you can break whenever an expression becomes True. You can break whenever a Public variable reaches a specific value, for example. You might want to do this when a Public or Private variable somehow is being changed, and you want to find out where. Consider the following code, located in the basFuncs module of CHAP15EX.MDB:Sub ChangeGlobal1()
gintCounter = 50
Call ChangeGlobal2
End Sub
Sub ChangeGlobal2()
gintCounter = gintCounter + 10
Call ChangeGlobal3
End Sub
Sub ChangeGlobal3()
Dim intCounter As Integer
For intCounter = 1 To 10
gintCounter = gintCounter + intCounter
Next intCounter
End Sub
You might find that gintCounter somehow is reaching a number greater than 100, and you are not sure how. To solve the problem, add the watch shown in Figure 15.17. Notice that the expression you are testing for is gintCounter > 100. You have set the breakpoint to break the code whenever the expression becomes True. To test the code, type ChangeGlobal1 in the Immediate window and press Enter. The code should break in the ChangeGlobal3 routine, indicating that this routine is the culprit.
Figure 15.17. This watch will cause the code execution to break whenever the expression is True.

Breaking When an Expression Changes
Instead of breaking when an expression becomes True, you might want to break whenever the value of the expression changes. This is a great way to identify the place where something is mysteriously modifying the value of a variable. Like Break When Value Is True, the Break When Value Changes option is great for tracking down problems with Public and Private variables. Notice the watch being set in Figure 15.18. It is in the context of all procedures within all modules. It is set to break whenever the value of gintCounter changes. If you execute the ChangeGlobal1 routine, you'll find that the code halts execution within ChangeGlobal1 immediately after the code sets the value of gintCounter to 50. If you press F5 to continue execution, the code halts within ChangeGlobal2 immediately after it increments the value of gintCounter by 10. In other words, every time the code modifies the value of gintCounter, the code execution breaks.
Figure 15.18. This watch will cause code execution to break whenever the value of an expression changes.
