I have a userform, in initial instance I have 7 checkboxes with a command button the user can create a new row with same numer of checkboxes (i.e., 7) and another button to delete the same row. Till here it works fine, my next stage is when the user checks one or all checkboxes from the newly created row, I am have difficulty in recording which checkbox was clicked and visa-versa.
I have used class module to trigger the events, it works perfectly when there are no predefined checkboxes in the form, but once I add the initial 7 checkboxes the msgbox displays the checkox23, checkbox99, checkbox278 as their names.
In userform I have this code -
Dim chkBoxEvent As clsBoxEvent
Dim chkBoxColl As Collection
Private Sub btnAddClass_Click()
Dim ctrl As Control, newCtrl As Control, offsetTop As Integer
Set chkBoxColl = New Collection
offsetTop = 36
For Each ctrl In Me.Controls
If TypeName(ctrl) <> "CommandButton" Then
If ctrl.Top = btnAddClass.Top - offsetTop Then
If TypeName(ctrl) = "ComboBox" Then
Set newCtrl = Me.Controls.Add("Forms.ComboBox.1")
ElseIf TypeName(ctrl) = "TextBox" Then
Set newCtrl = Me.Controls.Add("Forms.TextBox.1")
End If
nchk = 7
If TypeName(ctrl) = "CheckBox" Then
For i = 1 To nchk
Set newCtrl = Me.Controls.Add("Forms.Checkbox.1")
With newCtrl
.Height = ctrl.Height
.Width = ctrl.Width
.Top = ctrl.Top + offsetTop
.Left = ctrl.Left
.Tag = nchk * 10
End With
Set chkBoxEvent = New clsBoxEvent
Set chkBoxEvent.cBox = newCtrl
chkBoxColl.Add chkBoxEvent
Next
End If
If TypeName(newCtrl) <> "CheckBox" Then
With newCtrl
.Height = ctrl.Height
.Width = ctrl.Width
.Top = ctrl.Top + offsetTop
.Left = ctrl.Left
End With
End If
End If
End If
Next ctrl
btnAddClass.Top = btnAddClass.Top + offsetTop
btnRemoveClass.Top = btnRemoveClass.Top + offsetTop
Me.Height = Me.Height + offsetTop
End Sub
Private Sub btnRemoveClass_Click()
Dim ctrl As Control, offsetTop As Integer
offsetTop = 36
For Each ctrl In Me.Controls
If TypeName(ctrl) <> "CommandButton" Then
If ctrl.Top = btnAddClass.Top - offsetTop Then
Me.Controls.Remove (ctrl.Name)
End If
End If
Next ctrl
btnAddClass.Top = btnAddClass.Top - offsetTop
btnRemoveClass.Top = btnRemoveClass.Top - offsetTop
Me.Height = Me.Height - offsetTop
End Sub
And in Class Module -
Public WithEvents cBox As MSForms.CheckBox
Private Sub cbox_click()
msgbox cBox.Name & " clicked. Value is now " & IIf(cBox.Value, "Checked", "Unchecked")
End Sub
Bookmarks