访问 VBA CommandBarButton.OnAction 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10379813/
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
Access VBA CommandBarButton.OnAction doesn't work
提问by Daniel Olsen
I'm trying to create my own toolbar button (commandbarbutton) in Access VBA Editor. Its a button to run a procedure in a public module called "RunTests". I made the button like so:
我正在尝试在 Access VBA 编辑器中创建我自己的工具栏按钮 (commandbarbutton)。它是一个按钮,用于在名为“RunTests”的公共模块中运行过程。我像这样制作了按钮:
Public Sub CreateToolbar()
Dim cmdBar As CommandBar
Dim cmdButton As CommandBarButton
Set cmdBar = Application.VBE.CommandBars.Add("Run Tests")
Set cmdButton = cmdBar.Controls.Add(msoControlButton)
cmdButton.FaceId = 558
cmdButton.DescriptionText = "Run the tests"
cmdButton.Caption = "Run Tests"
cmdButton.OnAction = "RunTests"
cmdButton.Enabled = True
cmdButton.Style = msoButtonIconAndCaption
cmdBar.Visible = True
End Sub
I ran the procedure as a test. And it created the button just fine. But the "click" action doesn't respond. The button doesn't seem to be clickable. Does anybody know why?
我运行该程序作为测试。它很好地创建了按钮。但是“单击”操作没有响应。该按钮似乎不可点击。有人知道为什么吗?
Edit: Found the solution: http://support.microsoft.com/default.aspx?scid=kb;en-us;Q280607- I will answer my own question as soon as I can to close it.
编辑:找到解决方案:http: //support.microsoft.com/default.aspx?scid=kb; en- us; Q280607- 我会尽快回答我自己的问题并关闭它。
采纳答案by Daniel Olsen
Found the solution - To make it work you have to implement the workaround described here: http://support.microsoft.com/default.aspx?scid=kb;en-us;Q280607
找到解决方案 - 要使其工作,您必须实施此处描述的解决方法:http: //support.microsoft.com/default.aspx?scid=kb;en-us; Q280607
回答by Phil
The key for me was to make "RunTests" a public subroutine. When it was private it didn't work.
对我来说,关键是让“RunTests”成为一个公共子程序。当它是私人的时,它不起作用。
cmdButton.OnAction = "=RunTests()"
public sub RunTests()
private sub RunTests()
does not work.
private sub RunTests()
不起作用。
回答by Nelda.techspiress
Unfortunately I tried to be fancy and define my button and menus in a class. The OnAction subroutine was never executed while it was defined locally as part of the class. It would only execute when defined outside of the class definition.
不幸的是,我试图花哨并在课堂中定义我的按钮和菜单。OnAction 子例程在本地定义为类的一部分时从未执行。它只会在类定义之外定义时执行。
When I redefined my class to be just a module containing many subs and functions, then the OnAction subroutine was executed. Might have to do with scope and visibility or that I didn't have the OnAction semantics correct for a call to a class method. Bottom line is to define the button/menu/widget in a module and it will be much easier. (I was using Excel VBA but I think Access and Excel VBA are the same.)
当我将我的类重新定义为一个包含许多子函数和函数的模块时,就会执行 OnAction 子例程。可能与范围和可见性有关,或者我没有正确调用类方法的 OnAction 语义。底线是在模块中定义按钮/菜单/小部件,这会容易得多。(我使用的是 Excel VBA,但我认为 Access 和 Excel VBA 是一样的。)