进入2013 VBA - 设置为控制新的Click事件设置为、事件、VBA、Click

2023-09-08 11:10:40 作者:残云战神

我到处去寻找这一点,这似乎是一个简单的修复,但我似乎无法找到解决办法。我在访问2013形成几个矩形的控制,而且我创建处理他们所有的onclick事件。我在几个不同的方式工作,我想我发现这样做的最简单/干净的方式。我把控件集合中,改变OnClick事件为每个控件。这里是我的问题:访问打开表单,并承认我改变了事件的控制,但一旦我点击了控制,它抛出一个错误,不执行事件

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.

该错误:

的前pression在点击输入为事件属性设置   产生以下错误:您输入的EX pression有   函数名称,Microsoft Access找不到。

"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."

在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

另外,我在Form_Load事件中尝试一个简单的 shpTest.OnClick == TestClick(),并由此产生了同样的错误。任何人有什么想法?

Alternatively, I tried a simple shpTest.OnClick = "=TestClick()" in the Form_Load event, and this produced the same error. Anyone have any ideas?

推荐答案

该错误信息是告诉你无法找到一个功能命名的 TestClick 的。您的 TestClick 的是一个子程序,而不是一个函数。

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

下面是一个简单的例子,在Access 2010和2013年进行测试,这表明使用功能控件的 .OnClick 属性可以工作...但你需要一个功能。 : - )

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

请注意我的访问2013测试是与传统的桌面应用程序。如果你正在使用Access 2013 Web应用程序,我不知道会发生什么。

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.