Link to home
Start Free TrialLog in
Avatar of hindersaliva
hindersalivaFlag for United Kingdom of Great Britain and Northern Ireland

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.

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

Open in new window

Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Are you sure that you need to use a new class?

Can you show the code where you define the CheckGroup objects. please?
Please attach code of your UserForm1.frm and ClassModule.cls
Avatar of hindersaliva

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
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

Open in new window

You should be able to use the Parent function, which returns the container the control is in...

Me.Parent.Controls("lblMessage").Caption = "Your Message"
Wayne, no - that didn't work.
I think it's because, in this context VBA doesn't understand 'Me'.
I see the followin line in code of your UserForm:
Dim CheckBoxHandler As clsCheckBoxEvent

Open in new window


Where is that object set to an instance and which properties are set later in the code?
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

Open in new window

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" 

Open in new window

No, sorry Wayne. I get 'Could not find the specified object'. I checked the name of the control "lblMessage". Is correct.
ASKER CERTIFIED SOLUTION
Avatar of Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs)
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry. I'm closing an old question. I don't remember whether I solved it or how.