Capture dynamic click checkbox event

Asked

Viewed 418 times

2

I created a checkbox list as each category is entered into the database. However, I am unable to capture the click event of these dynamically created Checkboxes. Does anyone know where the error is?

Public Sub CarregarCheckBoxCategoria(Categoria As String)
Dim Cont As Integer

    Set chkCheckBox = frmProdutos.Frame3.Controls.Add("Forms.CheckBox.1", "NovoCheckBox", True)

    PositionTop = PositionTop + 12

    With chkCheckBox
        .Name = RemoverCaracter(Categoria)
        .Caption = Categoria
        .Width = 72
        .Height = 18
        .Top = .Top + PositionTop
        .Left = 186
    End With

End Sub

Private Sub NovoCheckBox_Click()
If CheckBox1.Value = True Then
    MsgBox "CheckBox has selected"
Else
    MsgBox "CheckBox has not selected"
End If
End Sub

I changed the above code, however, only the last checkbox runs the Click event. Follow my code:

Userform

Public Sub CarregarCheckBoxCategoria(Categoria As String)
Dim CheckBox As Control

ReDim CheckBoxs(iChk)


    Set CheckBox = frmProdutos.Frame3.Controls.Add("Forms.CheckBox.1", "NovoCheckBox", True)


    PositionTop = PositionTop + 12

    With CheckBox
        .Name = RemoverCaracter(Categoria)
        .Caption = Categoria
        .Width = 72
        .Height = 18
        .Top = .Top + PositionTop
        .Left = 186
    End With

    Set CheckBoxs(iChk).Ctrl = CheckBox

    iChk = iChk + 1
End Sub

Module

Option Explicit

Public CheckBoxs() As New CheckBoxHandler
Public iChk As Integer
Public PositionTop As Integer

Private Conn As ADODB.Connection
Private rst As ADODB.Recordset

Public Sub ExtrairCategoria()
iChk = 0

Dim strQuery As String
strQuery = "SELECT CategoryName FROM dbo.Categories ORDER BY CategoryName"
Set rst = New ADODB.Recordset
rst.Open strQuery, Conn, adOpenForwardOnly, adLockOptimistic

frmProdutos.cmbCategoria.Clear

Do While Not rst.EOF()
    frmProdutos.cmbCategoria.AddItem rst!CategoryName

    Call frmProdutos.CarregarCheckBoxCategoria(rst!CategoryName)

    rst.MoveNext
Loop

rst.Close
Set rst = Nothing

End Sub

In the Checkboxhandler Class

Option Explicit

Public WithEvents Ctrl As MSForms.CheckBox

Private Sub Ctrl_Click()

    MsgBox "Você clicou no CheckBox de nome " & Ctrl.Name

End Sub
  • 2

    I have the impression that yours ReDim is wrong. Compare in my original code there in the other question. It uses Resize only to set the final size of the array. In your case, you’re trying to make it grow dynamically (with each call). In this case, the array loses all existing content. You should have used Preserve on the call of ReDim (take a look in the documentation).

  • 2

    Another thing that seems wrong is that the ReDim expects the indexes or the size of the array. As you use the size, I think it should be iChk+1 (otherwise he’ll always have one less than necessary).

  • 1

    Luiz, I included the preserve and it worked perfectly. I will look at the other suggestion about size to see if this change will influence something. But the first suggestion already solved my problem. Thanks for your help!

1 answer

0


Kelly, look at this link that the form you made is not the proper one.

How to create events dynamically in VBA?

As you’ll see, your routine "Novocheckbox_click" must be part of a "class module".

See in the response of Luiz Vieira every step of what and how it should do, there is even the example of code for so much. I used exactly the way described by him and it worked perfectly.

Despite being another issue, you should create all at once, in a For Next, and not punctually, this way resolves because you will have to define or calculate the amount of dynamic objects to be created to start the For Next.

For this same amount of each group of objects, if working with groups, eliminating the previous group to process the new group (which may have a smaller size of items or larger, for example) the Redeem will be triggered once per group with this same amount (or she minus one starts from zero index to control from zero and not since one).

You can do it as soon as it works.

  • Leo, I tried to adapt the example you suggested to my code. It now identifies the click, but only from the last checkbox. I’ll edit my code so you can see where I’m going wrong.

  • 1

    Kelly, please, I don’t know if I have time right now, so I suggest you create a new question, because the solution to the first one has been forwarded and your question is now another, including new pieces of code. I’ll review it later if I don’t have time now.

  • Correct, but this question is DUPLICATED, see the link, the answer given by Luiz Vieira is extensive and complete, therefore, it was not the case to repeat what has already been answered. See also the comments. I helped with the answer exactly what was asked: WHERE IS THE ERROR? And I pointed out precisely.

  • 1

    It is not a question of who takes credit. Here on the site, the questions are not a dialogue, it is not to evolve as the doubt changes. When this happens, they become less and less useful to other people.

  • I agree, changing an answered question generating another, confuses who is researching and makes lose the sense of the answers given, besides discouraging those who dedicated themselves to answer initially. I will complement my answer in order to address the two questions asked here.

  • Answered the two questions.

  • 1

    Leo, I already solved the problem. It was just the lack of PRESERVE. Thanks for the help!

  • Okay, the effect is the same, because in both cases the new matrix to be treated is resized, in my case when creating the matrix zeroing its values to each new generated group (the matrix is created integrally), in your case "resizing" the matrix punctually, but preserving previous values (step by step). In both cases the resulting matrix meets the processing needs.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.