Watch Window

The last debugging tool you'll examine in this chapter is the Watch window, which enables you to watch a variable within your procedure. When the value of the variable changes or when the variable is True, your code enters break mode. To open the Watch window, select View O Watch Window.

To see how the Watch window works, use WatchGoShoppingSuits, a modified version of the GoShopping module. Recall that it uses a Boolean expression and message box to let you know if you're over budget. Add a watch on the blnOverBudget.Start by right-clicking in the Watch window and choosing Add Watch. The Add Watch dialog box opens (see Figure 4-9).

Enter blnOverBudget in the Expression text box. In the Watch Type, the default is to Watch Expression, although you could choose Break When Value is True, or Break When Value Changes. For this example, choose Break When Value Is True, and then click OK to save your watch. When you run the SteppingThroughCodeGoShopping2 procedure, the procedure enters break mode when the value of blnOverBudget becomes true. As soon as the loop executes for the eleventh time, the watch expression is triggered and the code enters break mode.

If you choose to simply watch the expression (rather than break), the Watch window behaves almost exactly like the Locals window except that only watched variables are shown.

Figure 4-9

If you have a rather long loop to execute and you no longer need your watch, you can delete it while your code is in break mode. Simply right-click the watch and select Delete Watch. You can then press F5 to continue code execution.

Option Compare Database Private blnOverBudget As Boolean Const curBudget = 1000

Private Sub GoShoppingSuits()

Dim intSuits As Integer Dim curSuitPrice As Currency Dim curTotalPrice As Currency

intSuits = InputBox("Enter the desired number of suits",

"Suits")

For i=1 To intSuits curTotalPrice = curTotalPrice + curSuitPrice

If curTotalPrice > curBudget Then blnOverBudget = True

Else blnOverBudget = False End If

Next

If blnOverBudget =

True Then

Msgbox "You're

over budget!"

, vbExclamation,

"Over Budget"

End If

End Sub

A shortcut for adding a watch is to right-click the expression and select Add Watch. The expression is filled in automatically, so you avoid the risk of typos.

Was this article helpful?

0 0

Post a comment