Watch Window

The last debugging tool we'll examine in this chapter is the watch window. This window allows you to watch a variable within your procedure. When the value of the variable changes or when the variable is

j Starj J ll<£> Ci Figure 5-7

True, your code will enter break mode. To display the Watch window, select Watch Window from the View menu.

We'll return to the CoffeeTime procedure we've used in other places in this chapter. If you want to know when the value of the intNumLattes variable changes, you can add a watch for that variable. To add a watch, right-click in the Watch window and choose Add Watch (Figure 5-9).

Enter intNumLattes in the Expression text box. Choose whether you want to watch the expression, break when the value is True, or break when the value changes. Click OK to save your watch. When you run the CoffeeTime procedure, the procedure will enter break mode whenever the value of the intNumLattes variable changes. If you choose to simply watch the expression (rather than break), the Watch window behaves almost exactly like the Locals window except that only the watched variables are shown.

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.

The CoffeeTime procedure isn't the best case study for the Watch window. If we alter the GoShopping procedure from earlier in this chapter just a bit, you can see the Watch window in action. Here's the revised code:

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 curSuitPrice = 100

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

Else blnOverBudget = False End If


If blnOverBudget = True Then

Msgbox "You're over budget!", vbExclamation, "Over Budget" End If End Sub

You can add a watch for the blnOverBudget and break when the value is True. As soon as your loop executes for the 11th time, the watch expression is triggered and the code enters break mode.

0 0

Post a comment