0
votes

I have a form with many buttons. Most of these buttons have a .tag field where I identify their function (numeric field)

Every button, on click, should execute same code, passing the .tag value to a function:

Private Sub CtlNameA_Click()
   Call WriteTimbraIN(CInt(CtlNameA.tag))
   DoCmd.Close
End Sub

Is there a smarter way to do it without writing the very same code (except for control name) in every button?

Thanks

2

2 Answers

2
votes

You can call a function from the form module directly from the button click events, and use Screen.ActiveControl to determine the calling button.

Private Function GenericButton()

    Debug.Print "Called from " & Screen.ActiveControl.Name

    Call WriteTimbraIN(CInt(Screen.ActiveControl.Tag))
    DoCmd.Close

End Function

And then assign =GenericButton() as OnClick event.
You can do this in one go by multi-selecting all buttons and then setting the property.

1
votes