Reputation: 17
I need to add buttons to a form at run time for Access. When clicked each button will need to open a modal form with specific information based on the button clicked. So for instance, if I write a loop to create five buttons and name them button1, button2, button3, button4 and button5; how would I achieve the following:
Private Sub button1_Click()
Msgbox "Button 1 was Clicked"
End Sub
Private Sub button2_Click()
Msgbox "Button 2 was Clicked"
End Sub
Private Sub button3_Click()
Msgbox "Button 3 was Clicked"
End Sub
Private Sub button4_Click()
Msgbox "Button 4 was Clicked"
End Sub
Private Sub button5_Click()
Msgbox "Button 5 was Clicked"
End Sub
This is a simplified version of what I want to achieve but this is the gist of it. Am I thinking about this correctly? Is there better way?
I am using MS Access 2010.
Any help is appreciated. Thanks
Upvotes: 1
Views: 2299
Reputation: 606
Here is how you can create an Event Procedure (Click()) for a Control (Command Button) named cmdDemo on the Current Form
Dim ctl As Control
Dim mdl As Module
Dim frm As Form
Set ctl = Me![cmdDemo] 'Set a Reference to the Command Button
Set frm = Me 'Set a reference to the Form
Set mdl = Me.Module 'Set a Reference to the Form's Code Module
'Create a Click() Event Procedure for the Command Button cmdDemo
lngReturn = mdl.CreateEventProc("Click", ctl.Name)
'Insert a single Line of Code in the Event Procedure
mdl.InsertLines lngReturn + 1, "Msgbox ""Create Procedure Demo"""
Upvotes: 2