Consulting

Results 1 to 6 of 6

Thread: Using a class module for several userforms, identify which userform is active

  1. #1
    Banned VBAX Newbie
    Joined
    Oct 2022
    Posts
    4
    Location

    Using a class module for several userforms, identify which userform is active

    Greetings gentlemen, I found this code, and the truth is that it works very well, it is a class module that creates a matrix for different controls (textboxes, combobox, listbox, etc.) it is really very versatile since you can assign different events For each control, I would like to use it with several userforms, for this I would need to verify which userform is loaded or active when the event is activated, I imagine it is with a function, but this must be at the class module level, thanks for any ideas you can contribute. ..


    'Inside the class module
    
    Private m_PassedControl As MSForms.Control
    Private WithEvents chk As MSForms.CheckBox
    Private WithEvents cbo As MSForms.ComboBox
    Private WithEvents lst As MSForms.ListBox
    Private WithEvents opt As MSForms.OptionButton
    Private WithEvents spn As MSForms.SpinButton
    Private WithEvents txt As MSForms.TextBox
    
    
    Property Set ctl(PassedControl As MSForms.Control)
    Set m_PassedControl = PassedControl
    
    
    Select Case TypeName(PassedControl)
    Case "CheckBox"
        Set chk = PassedControl
    Case "ComboBox"
        Set cbo = PassedControl
    Case "ListBox"
        Set lst = PassedControl
    Case "OptionButton"
        Set opt = PassedControl
    Case "SpinButton"
        Set spn = PassedControl
    Case "TextBox"
        Set txt = PassedControl
    End Select
    End Property
    
    
    Private Sub cbo_Change()
    PrintControlName
    End Sub
    
    
    Private Sub chk_Click()
    PrintControlName
    End Sub
    
    
    Private Sub lst_Change()
    PrintControlName
    End Sub
    
    
    Private Sub opt_Click()
    PrintControlName
    End Sub
    
    
    Private Sub spn_Change()
    PrintControlName
    End Sub
    
    
    Private Sub txt_Change()
    PrintControlName
    End Sub
    
    
    Sub PrintControlName()
    Debug.Print m_PassedControl.Name
    End Sub
    
    
    
    
    
    
    'Inside the userform 
    Public collControls As Collection
    Private cMultipleControls As clsMultipleControls
    
    
    Private Sub UserForm_Activate()
    Dim ctl As MSForms.Control
    
    
    Set collControls = New Collection
    For Each ctl In Me.Controls
        Set cMultipleControls = New clsMultipleControls
        Set cMultipleControls.ctl = ctl
        collControls.Add cMultipleControls
    Next ctl
    End Sub

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Not sure where you want to go with this but .Parent will give you the control's parent, which in MOST cases will be the UserForm.

    You might have to get the grand .Parent depending but the process is the same

    Option Explicit
    
    
    'Inside the class module
    
    
    Private m_PassedControl As MSForms.Control
    
    
    Private WithEvents chk As MSForms.CheckBox
    Private WithEvents cbo As MSForms.ComboBox
    Private WithEvents lst As MSForms.ListBox
    Private WithEvents opt As MSForms.OptionButton
    Private WithEvents spn As MSForms.SpinButton
    Private WithEvents txt As MSForms.TextBox
    
    
    
    
    Property Get frmName() As String
        frmName = m_PassedControl.Parent.Name
    End Property
    
    
    
    
    Property Set ctl(PassedControl As MSForms.Control)
        Set m_PassedControl = PassedControl
        
        
        Select Case TypeName(PassedControl)
            Case "CheckBox"
                Set chk = PassedControl
            Case "ComboBox"
                Set cbo = PassedControl
            Case "ListBox"
                Set lst = PassedControl
            Case "OptionButton"
                Set opt = PassedControl
            Case "SpinButton"
                Set spn = PassedControl
            Case "TextBox"
                Set txt = PassedControl
        End Select
    End Property
    
    
    
    
    Private Sub cbo_Change()
        PrintControlName
    End Sub
    
    
    
    
    Private Sub chk_Click()
        PrintControlName
    End Sub
    
    
    
    
    Private Sub lst_Change()
        PrintControlName
    End Sub
    
    
    
    
    Private Sub opt_Click()
        PrintControlName
    End Sub
    
    
    
    
    Private Sub spn_Change()
        PrintControlName
    End Sub
    
    
    
    
    Private Sub txt_Change()
        PrintControlName
    End Sub
    
    
    
    
    Sub PrintControlName()
        Select Case frmName
            Case "UserForm1"
                Debug.Print "Hello from UserForm1 --- ", m_PassedControl.Name
            Case "UserForm2"
                Debug.Print "Goodbye from UserForm2 --- ", m_PassedControl.Name
        End Select
    End Sub


    Hello from UserForm1 ---    CheckBox2
    Hello from UserForm1 ---    CheckBox1
    Goodbye from UserForm2 ---  OptionButton1
    Goodbye from UserForm2 ---  CheckBox1
    Goodbye from UserForm2 ---  CheckBox1

    HOWEVER, I personally don't think it's a good OOP idea to include such specific logic in an object. I'd look for a way to keep the object non-specific and have the calling procedure handle specific tasks
    Attached Files Attached Files
    Last edited by Paul_Hossler; 01-21-2023 at 01:35 PM.
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Since each Control in the UserForm Instantiates a new Class O)bject, Add to the Class Modue
    Private PF As Object
    
    Property Set ParentForm(Parent As Object)
       Set PF = Parent
    End Property
    In the userForm, "Set Controls" section Add the line
    cMultipleControls.ParentForm = Me
    Thereafter, I presume in the Class Module Code, If a Control needs to know its UserForm
    MyUserForm = Me.PF.Name
    Or it can directly access the UserForm with
    Me.PF(.Some Form Property)
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  4. #4

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    This is what confused me from the OP.

    I would like to use it with several userforms, for this I would need to verify which userform is loaded or active when the event is activated, I imagine it is with a function, but this must be at the class module level,
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,053
    Location
    Since this is not the first time that kapela2017 has been notified about cross posting here on this forum...... this thread is now closed. Kapela2017 needs to find a new home.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •