Timur Özalp
Timur Özalp

Reputation: 21

Pass a parameter to an event handler in Vba

I need to pass a parameter (ref or val doesn't matter) to an event associated to a control (checkbox) generated on runtime. I just can't seem to find a way. (I have to admit that i even tried a few dirty public declaration...to no avail) And on another note how can i access the properties of a control from the event?

The part where i generate the checkboxes:

For Each distinctClientList In Range("DA3:DA" & LastRow).Cells

    Dim MaTextBox As Object
    Set MaTextBox = Client_picking.Controls.Add("Forms.TextBox.1")
    With MaTextBox
        .Text = CStr(distinctClientList.Value)
        .Left = 20
        .top = topref + (20 * Client_picking.i)
        .Width = 90:
        .Height = 20
    End With

    Dim MaCheckBoxfile As Object
    Set MaCheckBoxfile = Client_picking.Controls.Add("Forms.CheckBox.1")
    With MaCheckBoxfile
        .Caption = "fichier"
        .Left = 140
        .top = topref + (20 * Client_picking.i)

    End With


    ReDim Preserve ButArray(1 To Client_picking.i)

    Set ButArray(Client_picking.i).butEvents() = MaCheckBoxfile
    Client_picking.i = Client_picking.i + 1
Next

My handler

Public WithEvents butEvents As MSForms.CheckBox    
Private Sub butEvents_click()If Checked Then
    MsgBox "checked" & /*This is where i would put my parameter... IF I HAD ONE!*/
End If

End Sub

Upvotes: 1

Views: 623

Answers (1)

jkpieterse
jkpieterse

Reputation: 3006

The control is assigned in the class to a variable named butEvents so you can access all its properties through it:

butEvents.BackColor = vbRed

Upvotes: 1

Related Questions