1

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.

5
  • Do the controls use basically the same code? Commented May 24, 2016 at 8:33
  • @Rory Yes they do! Commented May 24, 2016 at 9:04
  • Then this is basically a duplicate of this question: stackoverflow.com/questions/10592641/… Commented May 24, 2016 at 9:15
  • @rory But I don't need a CommandButton Commented May 24, 2016 at 9:29
  • I know but the principle is exactly the same for other control types. Commented May 24, 2016 at 9:30

2 Answers 2

4

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
Sign up to request clarification or add additional context in comments.

2 Comments

It's a bit difficult to understand, respective to combine it with my code.
@yuro I edited my answer and added an example that is more directly adpated to your code.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.