6. Call Stack
The Call Stack dialog box displays a list of
the current active procedure(s) when you are stepping through code. An
active procedure is one that is started but has not completed — and as
you are beginning to realize, the list can grow rather quickly. You can
access the Call Stack dialog box in several ways — you can press Ctrl+L
or use the menu bar (View =>
Call Stack). Because the call stack is available only in break mode,
access to the call stack is grayed out (disabled) when you are in Design
mode.
The Call Stack dialog box is a modal window and
therefore cannot be left open when stepping through code. So, after you
review the list of active procedures, you will need to close the dialog
box in order to continue running the code.
|
The Call Stack dialog box is most beneficial when one
procedure is calling another or if your code has nested procedures,
whether they are in the same module or being called by other modules.
When one procedure is called from another procedure, the dialog box
displays the new procedure at the top of the list with the original
(calling) procedure under it, thus the stacking effect. Figure 4 illustrates this stacking process. You will see that OverBudget was called by GoShopping, so OverBudget
is listed first, and it is highlighted because it is the procedure
being run. After a procedure is finished, it is removed from the stack.
In this case, after OverBudget is run, GoShopping will be the only procedure in the stack.
When you step through multiple procedures from
different modules, or even from the same module, it can be a bit
challenging to figuring out where a particular procedure is being
called. To help find the start of any active procedure in the call
stack, select the active (top) procedure in the list and either
double-click the item or click the Show button. In the current example,
the call stack was opened when OverBudget was called, so two procedures
are listed.
To find out what line called OverBudget, you can double-click on GoShopping, the calling procedure. This puts a green pointer at the line in GoShopping, which called OverBudget. Figure 5 shows OverBudget still highlighted, because that's the current point in stepping through the code, and the green pointer at the call to OverBudget.
As you can see, the call stack is helpful when you
are working with multiple procedures and trying to determine where
errant data may be originating. It is also a handy tool to be used in
conjuction with the other debugging tools when you need to decipher and
work with someone else's application or modules.
7. Run to Cursor
When testing your code, you will likely have many
times that you don't want to run every line of code line-by-line, but
executing the entire procedure at once isn't conducive to isolating and
identifying the source of a problem. And, if the code includes loops, it
can be very tedious to execute every line of the loop each time it
needs to run. For example, consider the loop (While ... Wend) in the following code:
Sub CoffeeTime()
Dim curLatteAllowance As Currency
Dim curLattePrice As Currency
Dim intNumLattes As Integer
Dim curTotalExpenses As Currency
curLattePrice = 3.5
curLatteAllowance = InputBox( _
"Enter the amount of money you have for lattes.", _
"Latte Allowance")
If curLattePrice <= curLatteAllowance Then
curTotalExpenses = curTotalExpenses + curLattePrice
Else
MsgBox "You do not have enough funds to buy a latte", _
vbOKOnly, "Total Lattes"
Exit Sub
End If
While curTotalExpenses < curLatteAllowance
intNumLattes = intNumLattes + 1
curTotalExpenses = curTotalExpenses + curLattePrice
Wend
Debug.Print intNumLattes
MsgBox "You can purchase " & intNumLattes & " lattes.", _
vbOkOnly, "Total Lattes"
End Sub
If you have $350 to spend on lattes, the While...Wend
loop will run 100 times. You do not want to have to press F8 to step
through the loop that many times. Thankfully, you have other options. If
you know that the loop is producing correct data, you can place your
cursor in the Debug.Print intNumLattes line and press Ctrl+F8 to invoke the Run to Curser. Pressing Ctrl+F8, will cause your procedure to run until it reaches the Debug.Print
line; at that point, it will halt and the line will be highlighted. You
can then press F8 to step through and execute just the highlighted line
of code or press F5 to continue execution until the end of the
procedure.