Here is a little hack. You need Form1, Command1 on it, and Class1. Paste as following:
Code:
' Class1
Option Explicit
Dim m_CallType As VbCallType
Dim m_ClickFunction As String
Dim m_Parameters() As Variant
Dim m_Parent As Object
Public WithEvents Button As CommandButton
Public Function Init(ByRef Parent As Form, ByRef ClickFunction As String, ByVal CallType As VbCallType, ParamArray Parameters()) As CommandButton
If Not Parent Is Nothing Then
m_CallType = CallType
m_ClickFunction = ClickFunction
m_Parameters = Parameters
Set m_Parent = Parent
Set Button = Parent.Controls.Add("VB.CommandButton", "Cmd" & Parent.Controls.Count)
Set Init = Button
End If
End Function
Private Sub Class_Terminate()
Set Button = Nothing
End Sub
Private Sub Button_Click()
CallByName m_Parent, m_ClickFunction, m_CallType, m_Parameters
End Sub
Code:
' Form1
Option Explicit
Dim Test As New Collection
Public Sub ButtonClick(ByRef Parameters As Variant)
MsgBox Parameters(0)
End Sub
Private Sub Command1_Click()
Dim Reference As Class1
Test.Add New Class1
Set Reference = Test(Test.Count)
With Reference.Init(Me, "ButtonClick", VbMethod, Test.Count)
.Move 0, 360 * (Test.Count - 1), 3600, 360
.Visible = True
End With
End Sub
Booya, you can create as many command buttons as you like, you can set what parameters are passed to the click event on the form (there can be more than one!) and this enables you to have custom code that depends on what button was clicked.