如何使用 VBA 在 运行 时间将按钮添加到 MS Access 表单并将代码添加到 _Click() 事件
How do I add buttons to an MS Access form at run time with VBA and add code to the _Click() event
我需要在 运行 时向表单添加按钮以进行访问。单击时,每个按钮都需要打开一个模式表单,其中包含基于单击的按钮的特定信息。例如,如果我编写一个循环来创建五个按钮并将它们命名为 button1、button2、button3、button4 和 button5;我将如何实现以下目标:
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
这是我想要实现的目标的简化版本,但这是它的要点。我在想这个吗?有没有更好的办法?
我正在使用 MS Access 2010。
感谢任何帮助。谢谢
以下是如何为当前窗体上名为 cmdDemo 的控件(命令按钮)创建事件过程 (Click())
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"""
我需要在 运行 时向表单添加按钮以进行访问。单击时,每个按钮都需要打开一个模式表单,其中包含基于单击的按钮的特定信息。例如,如果我编写一个循环来创建五个按钮并将它们命名为 button1、button2、button3、button4 和 button5;我将如何实现以下目标:
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
这是我想要实现的目标的简化版本,但这是它的要点。我在想这个吗?有没有更好的办法?
我正在使用 MS Access 2010。
感谢任何帮助。谢谢
以下是如何为当前窗体上名为 cmdDemo 的控件(命令按钮)创建事件过程 (Click())
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"""