0

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 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

Yes, use WithEvents. An example can be found here:

Create Windows Phone Colour Palette and Selector using WithEvents

1 Comment

It's an elegant solution, but Andre's solution is cleaner, thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.