0

I have tried @DaveShaw code, for events on runtime for checkboxes, is click not an valid method for checkbox? It never get into the method checkBoxEvent_click

Dim CheckBoxArray() As New ClassEvents          
for i=0 to 10  
          Set cTemp = MOM.Frame_MOM_MOM.Controls.Add("Forms.CheckBox.1")

            With cTemp
                .Top = HeaderOffset + RowOffset + i * 25 'your top pos
                .Visible = True
                .Left = 30  'your left pos
                .Width = widthOfLabel 'your width
                .Name = Replace(keyArrays(i, 1), " ", "_")
                .Caption = keyArrays(i, 1) 'your caption ,


            End With

            ReDim Preserve CheckBoxArray(0 To i)
            Set CheckBoxArray(i).checkBoxEvent = cTemp
            next i

and my ClassEvents class looks like this:

Public WithEvents checkBoxEvent As MSForms.checkBox

Private Sub checkBoxEvent_click()
    MsgBox "halla" 'checkBox.Caption
End Sub
10
  • "is click not an valid method for checkbox?" Correct: msdn.microsoft.com/en-us/library/… Commented Jul 25, 2016 at 13:37
  • 1
    @Tim, in VBA it IS Commented Jul 25, 2016 at 13:49
  • Theoretically, your code will work ... could you put Option Explicit at the top of your module and remove any On Error Resume Next lines if you have them. Commented Jul 25, 2016 at 14:03
  • @user3598756 I stand corrected. when adding a MSForms.CheckBox, there is a click event: msdn.microsoft.com/en-us/library/office/…. It is a Windows.Forms checkbox that does not expse the click event. Commented Jul 25, 2016 at 14:11
  • @Ambie "Theoretically, your code will work" provided that CheckBoxArray() is a Userfom wide scoped variable, thus to be placed at the very top of the Userfom code pane and so outside any Subs/Function code Commented Jul 25, 2016 at 14:22

1 Answer 1

1

you have to keep Dim CheckBoxArray() As New ClassEvents at the very top of your userfom code pane, thus outside any of its subs/functions

furthermore use Option Explicit statement too

it becomes

Option Explicit

Dim CheckBoxArray() As New ClassEvents '<--| keep this line at the very top of your userform code pane

Private Sub UserForm_Initialize()
    Dim i As Long
    Dim cTemp As MSForms.CheckBox '<-- with "Option Explicit" you have to declare all your variables

    For i = 0 To 10
        Set cTemp = MOM.Frame_MOM_MOM.Controls.Add("Forms.CheckBox.1")
          With cTemp
             .Top = HeaderOffset + RowOffset + i * 25 'your top pos
              .Visible = True
              .Left = 30  'your left pos
              .Width = widthOfLabel 'your width
              .Name = Replace(keyArrays(i, 1), " ", "_")
              .Caption = keyArrays(i, 1) 'your caption ,
          End With
          ReDim Preserve CheckBoxArray(0 To i)
          Set CheckBoxArray(i).checkBoxEvent = cTemp
    Next i
End Sub

furthermore, since you already know the dimension of your array, Dim it at the beginning and don't ReDim it at every iteration:

Option Explicit

Dim CheckBoxArray(0 To 10) As New ClassEvents '<--| keep this line at the very top of your userform code pane

Private Sub UserForm_Initialize()
    Dim i As Long
    Dim cTemp As MSForms.CheckBox '<-- with "Option Explicit" you have to declare all your variables

    For i = 0 To 10
        Set cTemp = MOM.Frame_MOM_MOM.Controls.Add("Forms.CheckBox.1")
          With cTemp
             .Top = HeaderOffset + RowOffset + i * 25 'your top pos
              .Visible = True
              .Left = 30  'your left pos
              .Width = widthOfLabel 'your width
              .Name = Replace(keyArrays(i, 1), " ", "_")
              .Caption = keyArrays(i, 1) 'your caption ,
          End With
          Set CheckBoxArray(i).checkBoxEvent = cTemp
    Next i
End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

So, making it explicit helped. I do not know the size of the array(input is coming from DB, i stripped the code). What I want to achieve is to find out which order the checkboxes where clicked in, then make a string with the name of the checkboxes in order of click appearance. I will put Dim CheckBoxArray(0 To 10) As New ClassEvents in my setting class where I keep all my global variables.

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.