Access Reuse a Button

If I am using the same button on the main form and linked subforms, all on a tab form, please offer how I can copy and paste the same button, without having to modify the code and give the button a new name on each of the subforms.

Who is Participating?
MNelson831Connect With a Mentor Commented:
Copy the code as a function and then call the function instead of repeating the code in every button.

Private Function DoCoolStuff(intID as integer, stData as string)

<execute code here>

End Function

Private Sub Command1_OnClick()
DoCoolStuff me.MyId, Me.Data
End Sub

Private Sub Command2_OnClick()
DoCoolStuff me.MyId, Me.Data
End Sub

Private Sub Command3_OnClick()
DoCoolStuff me.MyId, Me.Data
End Sub

Private Sub Command4_OnClick()
DoCoolStuff me.MyId, Me.Data
End Sub
tahirihAuthor Commented:
Thank you.
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
  which is free, has a copy controls with code function.  Outside of that, there is nothing in Access/VBA that allows you to copy a button.
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
Unfortunately, when you copy and paste a control with code behind, you lose the code.  So, you will have to get the code separately ...

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.