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