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 not completed. You can access the Call Stack dialog box in several ways, including from the menu bar (View the Call Stack) or by pressing Ctrl+L. Because the call stack is available only in break mode, access to the call stack is often grayed out (disabled).

The Call Stack dialog box is not a window and therefore cannot be left open when stepping through code. It is opened and closed at each active procedure.

You gain the most benefit from the Call Stack dialog box when one procedure is calling another or if you have nested procedures, whether they are in the same module or being called by other modules. If another procedure is called from the first procedure, the dialog box displays the new procedure at the top of the list with the original (calling) procedure under it, thus stacking them. Figure 4-6 illustrates this stacking process. OverBudget was called by GoShopping, so OverBudget is listed first, and it is highlighted because it is the procedure being run. Once 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.

I Over Budget

Dim curTotalPrice A3 Currency curSuitPrice = 300

intSuits = InputBox("Enter the desired number of suits". For i = 1 To intSuits curTotalPrice = curTotalPrice + curSuitPrice If curTotalPrice > curBudget Then binUnderBudget = False

Else binUnderBudget = True End If

Next

If binUnderBudget = False Then

OverBudget End If

End Sub_

Private Sub OverBudget() Debug.Print "You've gone over budget." Debug.Print "You need to work some overtime." Debug.Print "Remember to pay your taxes." End Sub

"3

Call Stack

Project. Module. Function

Chapter4VB AEditor, Ca

Expre

Chapter4VB AEditor, CallStackDemo. OverBudget

Chapter4VBAEditor, CallStackDemo. GoShopping

Figure 4-6

When stepping through multiple procedures from different modules, or even from the same module, it can be a little confusing as to where a particular procedure is being called. To help find the start of the any active procedure in the call stack, highlight 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 that called OverBudget. Figure 4-7 shows OverBudget still highlighted in yellow, because that's the current point in stepping through the code, and the green pointer at the call to OverBudget.

TI J GoShopping

Dim curTotalPrice As Currency curSuitPrice = 300

intSuits = InputBox("Enter the desired number of suits", "Suits"} For i = 1 To intSuits curTotalPrice = curTotalPrice + curSuitPrice If curTotalPrice > curBudget Then binUnderBudget = False

Else binUnderBudget = True End If

Next

If binUnderBudget = False Then | OverBudget End If End Sub

Private Sub OverBudget0 Debug.Print "You've gone over budget." Debug.Print "You need to work some overtime. Debug.Print "Remember to pay your taxes." End 5ub

As you might imagine, 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 when working with someone else's application or even modules.

Was this article helpful?

0 0

Responses

  • noora
    How to view call stack from vba?
    8 years ago

Post a comment