hindersaliva
asked on
VBA - UserForm and trapping Event with a Class Module
I have a Label control "lblMessage" on UserForm1. There's a CheckBox1 "chkM01" control on it. When the CheckBox is clicked I want "lblMessage" to show a message.
Ok, that's simple enough.
Where I'm stuck is, the CheckBox control's click event is run by a Class Module. Here's the code. And the form could be any one of 3 UserForms. So, I can't hard code it to frmUserForm1, as it can be 2 or 3..
I tried Me.lblMessage.Caption = "Message1" but didn't expect it to work. It didn't!
Thanks for your help.
Ok, that's simple enough.
Where I'm stuck is, the CheckBox control's click event is run by a Class Module. Here's the code. And the form could be any one of 3 UserForms. So, I can't hard code it to frmUserForm1, as it can be 2 or 3..
I tried Me.lblMessage.Caption = "Message1" but didn't expect it to work. It didn't!
Thanks for your help.
Option Explicit
Public WithEvents CheckGroup As MSForms.CheckBox
Private Sub CheckGroup_Click()
Dim strItemID As String
strItemID = Right(CheckGroup.Name, Len(CheckGroup.Name) - 3)
If CheckGroup.Value = True Then
Call HideSelectedItem(strItemID, "Show")
Call UpdateArrItemsAll(strItemID, "Shown")
'Now show a message in lblMessage <<<<<<<<<<<<<<<<<<<<<<<<<<<
Else
Call HideSelectedItem(strItemID, "Hide")
Call UpdateArrItemsAll(strItemID, "Hidden")
'Now show a message in lblMessage <<<<<<<<<<<<<<<<<<<<<<<<<<<
End If
End Sub
Please attach code of your UserForm1.frm and ClassModule.cls
ASKER
Graham, I need a Class Module because the UserForm is populated with controls dynamically. The controls have an ID in their Name. This ID is used in the code that runs when the control is clicked.
This is the code in UserForm_Initialize
This is the code in UserForm_Initialize
Dim CheckBoxesColl As Collection
And in the Sub
'for CheckBox events
Dim CheckBoxHandler As clsCheckBoxEvent
Set CheckBoxesColl = New Collection
Dim cCont3 As Control
and looping through an array of items
With cCont3
.Caption = ""
.AutoSize = True
.Top = i * 22
.Left = 365
.Name = "ckb" & arrSections(1, i)
.Value = True
End With
You should be able to use the Parent function, which returns the container the control is in...
Me.Parent.Controls("lblMes sage").Cap tion = "Your Message"
Me.Parent.Controls("lblMes
ASKER
Wayne, no - that didn't work.
I think it's because, in this context VBA doesn't understand 'Me'.
I think it's because, in this context VBA doesn't understand 'Me'.
I see the followin line in code of your UserForm:
Where is that object set to an instance and which properties are set later in the code?
Dim CheckBoxHandler As clsCheckBoxEvent
Where is that object set to an instance and which properties are set later in the code?
ASKER
Mate, it's in the Form Initialize (sorry, I should have said)
For Each ctItem In Me.Controls
If TypeName(ctItem) = "CheckBox" Then
Set CheckBoxHandler = New clsCheckBoxEvent
Set CheckBoxHandler.CheckGroup = ctItem
CheckBoxesColl.Add CheckBoxHandler
End If
Next
Wayne, no - that didn't work.
I think it's because, in this context VBA doesn't understand 'Me'.
What about this then?
CheckGroup.Parent.Controls("lblMessage").Caption = "Your Message"
ASKER
No, sorry Wayne. I get 'Could not find the specified object'. I checked the name of the control "lblMessage". Is correct.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sorry. I'm closing an old question. I don't remember whether I solved it or how.
Can you show the code where you define the CheckGroup objects. please?