Access 2013 VBA - 为控件设置新的点击事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26428818/
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 2013 VBA - Setting New Click Event for Controls
提问by JaredS
I have searched everywhere for this, and it seems like a simple fix, but I can't seem to find the solution. I have several Rectangle controls in my Access 2013 form, and I'm creating an OnClick event that handles them all. I've worked on a few different methods, and I think I found the simplest/cleanest way to do it. I put the controls in a collection and change the OnClick event for each control. Here's my problem: Access opens the form and recognizes that I changed the event for the control, but once I click the control, it throws an error and will not execute the event.
我到处搜索这个,这似乎是一个简单的修复,但我似乎无法找到解决方案。我的 Access 2013 表单中有几个 Rectangle 控件,我正在创建一个处理它们的 OnClick 事件。我研究了几种不同的方法,我想我找到了最简单/最干净的方法。我将控件放在一个集合中并更改每个控件的 OnClick 事件。这是我的问题:Access 打开窗体并识别出我更改了控件的事件,但是一旦我单击该控件,它就会引发错误并且不会执行该事件。
The Error:
错误:
"The expression On Click entered as the event property setting produced the following error: The expression you entered has a function name that Microsoft Access can't find."
“作为事件属性设置输入的表达式 On Click 产生以下错误:您输入的表达式具有 Microsoft Access 找不到的函数名称。”
The Code:
编码:
Private Sub Form_Load()
Dim m_colRectangle As Collection
Dim ctl As Access.CONTROL
Set m_colRectangle = New Collection
For Each ctl In Me.Controls
If ctl.ControlType = acRectangle Then
If ctl.Name = "shpTest" Then
m_colRectangle.Add ctl, ctl.Name
ctl.OnClick = "=TestClick()" ' <--- Error on this line
End If
End If
Next ctl
End Sub
Private Sub TestClick()
MsgBox "Test"
End Sub
Alternatively, I tried a simple shpTest.OnClick = "=TestClick()"
in the Form_Load event, and this produced the same error. Anyone have any ideas?
或者,我shpTest.OnClick = "=TestClick()"
在 Form_Load 事件中尝试了一个简单的方法,这产生了同样的错误。谁有想法?
回答by RubberDuck
You don't get to specify which procedure runs in VBA. The procedure that runs will alwaysbe ControlName_Click
. The OnClick
property that you're trying to set only lets you switch between Access Macro and [Event Procedure]
vba code. It does not work like event delegates do in the .Net platform.
您无法指定在 VBA 中运行哪个过程。运行的过程将始终是ControlName_Click
. OnClick
您尝试设置的属性仅允许您在 Access 宏和[Event Procedure]
vba 代码之间切换。它不像 .Net 平台中的事件委托那样工作。
Please see the OnClick Property documentation on MSDN.
请参阅MSDN 上的OnClick 属性文档。
The solution here is to use the Microsoft Visual Basic for Applications Extensibilty Library to write snippets of code into the modules. I'll leave that as as exercise for you.
此处的解决方案是使用 Microsoft Visual Basic for Applications Extensibilty Library 将代码片段写入模块。我会把它留给你作为练习。
回答by HansUp
The error message is telling you Access can't find a functionnamed TestClick. Your TestClickis a subroutine, not a function.
错误消息告诉您 Access 找不到名为TestClick的函数。您的TestClick是一个子程序,而不是一个函数。
Here is a simpler example, tested in Access 2010 and 2013, which demonstrates that using a function for a control's .OnClick
property can work ... but you need a function. :-)
这是一个更简单的示例,在 Access 2010 和 2013 中进行了测试,它演示了对控件的.OnClick
属性使用函数可以工作……但是您需要一个函数。:-)
Private Sub Form_Load()
Dim ctl As Control
Set ctl = Me.Controls("txtMathExpresson")
ctl.OnClick = "=TestClick()"
Set ctl = Nothing
End Sub
Private Function TestClick()
MsgBox "Test"
End Function
Note my Access 2013 test was with a traditional desktop application. If you're working with an Access 2013 WebApp, I don't know what will happen.
请注意,我的 Access 2013 测试是使用传统桌面应用程序进行的。如果您使用的是 Access 2013 WebApp,我不知道会发生什么。