vba 哪个 SUB 调用这个 SUB

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17038940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 21:34:59  来源:igfitidea点击:

Which SUB is calling this SUB

vbams-access

提问by Richard Spencer

Does anyone out there know how to do a stack trace in access-vba. I'm trying to do something like:

有没有人知道如何在access-vba 中进行堆栈跟踪。我正在尝试执行以下操作:

Public Sub a()   
    Call c    
End Sub    

Public Sub b()    
    Call c    
End Sub

Public Sub c()    
    Debug.Print "Which sub has called me ?"    
End Sub

What I want to do in Sub cis to show if that has been called by Sub aor Sub bwithout passing any arguments. In cI would simply show the stack but I have no idea if this is even possible in VBA - any thoughts ?

我想要做的Sub c是显示它是否已被调用Sub aSub b不传递任何参数。在c 中,我会简单地显示堆栈,但我不知道这在 VBA 中是否可行 - 有什么想法吗?

回答by Francis Dean

You can access the call stack during runtime under the menu View-> Call Stack

您可以在运行时在菜单下访问调用堆栈View->Call Stack

Alternatively you can use the keyboard shortcut CTRL+Lduring runtime.

或者,您可以在运行时使用键盘快捷键CTRL+ L

enter image description here

在此处输入图片说明

回答by Santosh

You can use Mztools Addins which has an option to view the procedure caller. Download Mztools

您可以使用 Mztools 插件,它有一个选项来查看过程调用者。 下载 Mztools

enter image description here

在此处输入图片说明

回答by Jon Fournier

The way I do this is by having a ProcedureEnter and ProcedureExit call at the beginning and end of each VBA routine I have. The ProcedureEnter routine has an argument for the Sub/Function name, which gets stored in a global call stack collection. ProcedureExit just pops the last entry off the stack.

我这样做的方法是在我拥有的每个 VBA 例程的开头和结尾处调用 ProcedureEnter 和 ProcedureExit。ProcedureEnter 例程有一个用于 Sub/Function 名称的参数,该参数存储在全局调用堆栈集合中。ProcedureExit 只是从堆栈中弹出最后一个条目。

So, to get the caller routine name, you would just get the item at the Stack Collection .count - 1

因此,要获得调用例程名称,您只需在 Stack Collection .count - 1 中获得该项目

I personally use MZTools to set up a default VBA routine header/footer, so that I just type

我个人使用 MZTools 来设置默认的 VBA 例程页眉/页脚,因此我只需键入

Function fnname(arg as whatever) as boolean
End Function

and click the Add Error Handler button in MZTools and it adds the ProcedureEnter and ProcedureExit calls for me. That makes it a lot less cumbersome to add the stack trace code.

然后单击 MZTools 中的 Add Error Handler 按钮,它为我添加了 ProcedureEnter 和 ProcedureExit 调用。这使得添加堆栈跟踪代码变得不那么麻烦。

回答by Meredith Poor

The only way you could tell would be to get the return address pointer of the calling subroutine from the stack. VBA wouldn't do that directly, you would have to have written your own library or have a library from someone else who needed to know these things. This might be worthwhile if the calling routines are from a program you can't update, however if this is only coming from your own code, it's far easier to rewrite your code to identify the caller.

您可以判断的唯一方法是从堆栈中获取调用子例程的返回地址指针。VBA 不会直接这样做,您必须编写自己的库或从需要了解这些事情的其他人那里获得库。如果调用例程来自您无法更新的程序,这可能是值得的,但是如果这仅来自您自己的代码,则重写代码以识别调用者要容易得多。