yuro
yuro

Reputation: 2209

Dynamically define the events of the checkboxes in VBA

I have defined dynamically the checkboxes for my UserForm. See the code:

If rs.EOF = False Then
    i = 1
    Do Until rs.EOF Or i = 6
        With frmOne.Controls("Version" & i)
            .Visible = True
            .Caption = rs!versNo & "#" & rs!Vers_From
            .tag = rs!versNo & "_" & rs!noID & ".pdf"
        End With
        i = i + 1
        rs.MoveNext
    Loop
End If

Well, for these checkboxes I need a "Click"-Event. For example:

Private Sub Version1_Click()
   If FilterOk = True Then
       VersNr = Mid(frmOne.Version1.tag, 1, InStr(frmOne.Version1.tag, "_") - 1)
       Call funcVersion
       Exit Sub
   End If
   ...
End Sub

How can I make dynamic? I don't need a CommandButton for that. That means, when the user click on the Checkbox then the eventhandler starts.

Upvotes: 1

Views: 3329

Answers (2)

Petr Srníček
Petr Srníček

Reputation: 2386

Rory is right about this being a duplicate, but since I already wrote a little snippet, I will post it here. I hope that it is not against the rules. You need to create a custom class with an event-enabled object that will handle the events. You can then assign reference to your checkboxes to these objects.

A simple example for demonstration:

Create a class module named CheckBoxEventHandler and place the following code inside the class module.

' This will store a reference to a checkbox and enable handling its events.
Private WithEvents m_chckBox As MSForms.CheckBox

' Method to assign a reference to a checkbox to your event handler
Public Sub AssignCheckBox(c As MSForms.CheckBox)
    Set m_chckBox = c
End Sub

' Private sub to execute something on the event
Private Sub m_chckBox_Click()
    MsgBox "Checkbox" + m_chckBox.Caption + "clicked"
End Sub

Create a userform with some checkboxes and place the following code in its module:

' Define a collection to store your event handlers while the userform is active.
Private eventHandlerCollection As New Collection

Private Sub UserForm_Initialize()
    Dim chckBoxEventHandler As CheckBoxEventHandler, c As Control

    For Each c In UserForm1.Controls
        If TypeName(c) = "CheckBox" Then
            'Create event handler instance
            Set chckBoxEventHandler = New CheckBoxEventHandler
            'Assign it reference to a checkbox
            chckBoxEventHandler.AssignCheckBox c
            'Store the event handler in the userform's collection,
            eventHandlerCollection.Add chckBoxEventHandler
        End If
    Next
End Sub



Here is a possible way to implement this in your case

(I don't have your exact code so I was not able to test it, but I believe that it should give you the general idea.)

1. Create a new class named CheckboxEventHandler

Public WithEvents chckBox As MSForms.CheckBox

Private Sub chckBox_Click()
    Debug.Print "Checkbox" + chckBox.Caption + "clicked"
    ' Do your click-handler logic here.
    ' If you need private variables that are defined elsewhere, you can define the function
    ' whereever you need it and use the eventhandler only to call it and pass it a reference to the clicked checkbox:
    Call somefunction(chckBox)
    ' Or you could define the function as a public method in frmOne and call it from here like this:
    Call frmOne.somefunction(chckBox)
End Sub

2. Add the following at the beggining of the code in your frmOne userform:

' Define a collection to store event handlers.
Private eventHandlerCollection As New Collection

' Method for adding clickhandlers to checkBoxes dynamically
Public Sub createClickHandler(c As MSForms.CheckBox)
    Dim eventHandler As New CheckBoxEventHandler
    eventHandler.chckBox = c
    Call eventHandlerCollection.Add(eventHandler)
End Sub

3. Attach the event handlers to the checkboxes

If rs.EOF = False Then
    i = 1
    Do Until rs.EOF Or i = 6

        With frmOne.Controls("Version" & i)
            .Visible = True
            .Caption = rs!versNo & "#" & rs!Vers_From
            .Tag = rs!versNo & "_" & rs!noID & ".pdf"
        End With

        'register event listener
        frmOne.createClickHandler (frmOne.Controls("Version" & i))
        i = i + 1
        rs.MoveNext
    Loop
End If

Upvotes: 4

Thomas G
Thomas G

Reputation: 10206

Use the AfterUpdate event of your checkboxes

Private Sub MyCheckBox_AfterUpdate()

   ' If the checkbox is unchecked, do nothing
   If MyCheckbox = False Then Exit Sub  

   ' If the checkbox is checked, do whatever you want:
   If FilterOk = True Then
       VersNr = Mid(frmOne.Version1.tag, 1, InStr(frmOne.Version1.tag, "_") - 1)
       Call funcVersion
       Exit Sub
   End If
   ...
End Sub

Upvotes: 0

Related Questions