Access 2013 VBA - Setting New Click Event for Controls

亡梦爱人 提交于 2019-12-05 08:27:49

The error message is telling you Access can't find a function named TestClick. Your TestClick is a subroutine, not a function.

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. :-)

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.

RubberDuck

You don't get to specify which procedure runs in VBA. The procedure that runs will always be 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.

Please see the OnClick Property documentation on MSDN.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!