Search code examples
vbams-access

MS Access: Replace Many On Click Events with Generic On Click Event


I have a form in MS Access with 12 buttons running along the top serving as column headers. For each button there is an On Click event which calls the same public function. This function, (shown below), opens the Filter Menu for the field corresponding to the button that was clicked.

Public Function HeaderClick(HeaderName As String) 
    DoCmd.GoToControl "[" & HeaderName & "]"
    DoCmd.RunCommand acCmdFilterMenu
End Function

For example, I click on the First Name button and it pulls up the filter menu for First Name: enter image description here

I'm wondering if there is a way to call HeaderClick whenever ANY of these buttons are clicked. In other words, I'd like an alternative to creating 12 separate On Click Events. Reason being that this is a technique I use on many forms in many databases. It just doesn't seem very efficient.


Solution

  • When you click on the OnClick textbox to add a new function, you typically see the text [Event Procedure]. Just overwrite it with the name of the function (make sure the function is in a global module). So instead of [Event Procedure], you would type =HeaderClick("Name"). You would still need to specify the argument for the function.