+ Reply to Thread
Results 1 to 3 of 3

get active userform in class module

Hybrid View

  1. #1
    Registered User
    Join Date
    11-25-2024
    Location
    Italy
    MS-Off Ver
    Office 365
    Posts
    13

    get active userform in class module

    the following code create three userforms, each one containing a commandbutton defined in a class module and, when clicked on an userform, it should cycle over all controls of that userform. How can I understand on which userform the user has clicked the commandbutton? I don't know if there is a simple solution or it can be done for example using API functions.

    this is the code in the userform named Userform1 (add the commandbutton to the userform):
    Private Sub UserForm_initialize()
    
        Dim cb_class As Class_CmdButton
        Dim cb_cmd As MSForms.CommandButton
        
        Set cb_cmd = Me.Controls.Add("forms.commandbutton.1", "CommandButton1")
        cb_cmd.Move 50, 50, 120, 30
        Set cb_class = New Class_CmdButton
        Set cb_class.CmdButton = cb_cmd
        CDict.Add cb_class, cb_cmd
    
    End Sub
    this is the code in a standard module (create the userforms):
    Sub openuserform()
    
    Dim MyUF As Object
    Dim MyUFname As String
    
    Set CDict = CreateObject("Scripting.dictionary")
    
    For i = 1 To 3
        Set MyUF = ActiveWorkbook.VBProject.VBComponents.Add(vbext_ct_MSForm)
        MyUFname = MyUF.Name
        VBA.UserForms.Add (MyUFname)
        Set MyUF = New UserForm1
        MyUF.Show vbModeless
    Next
    
    End Sub
    and this is the code in the class module named Class_CmdButton (where i have the problem)
    Private WithEvents CMB As MSForms.CommandButton
    
    Private ctrl As Control
    Private UF As UserForm
    
    Public Property Set CmdButton(ByVal cmd As MSForms.CommandButton)
        Set CMB = cmd
        CMB.Caption = "Caption CmdButton"
    End Property
    
    Private Sub CMB_Click()
    
        Set UF= 'HOW CAN I GET ACTIVE USERFORM?
        For Each ctrl In UF.Controls
            MsgBox F.Caption, ctrl.Name
        Next
    
    End Sub
    thanks in advance for your help

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: get active userform in class module

    If it's just on the form, then CMB.Parent should give you a reference to the form, but I would personally add another property to the class to hold a reference to the form, and set it when you set the button.
    Everyone who confuses correlation and causation ends up dead.

  3. #3
    Registered User
    Join Date
    11-25-2024
    Location
    Italy
    MS-Off Ver
    Office 365
    Posts
    13

    Re: get active userform in class module

    Sorry for my late answer, .parent works perfectly, i've also tried to use a property but should i use .parent also in this case to reference the form? It's not completely clear to me how to handle the property in this case
    thanks

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Using a class module for several userforms, identify which userform is active
    By kapela in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-24-2023, 09:18 PM
  2. [SOLVED] Class module for userform textboxes
    By Knoet in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 06-04-2020, 06:59 PM
  3. Place code in userform, module or class module
    By nigelog in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-16-2017, 07:04 AM
  4. Advice on passing userform parameters to a vba class module
    By techno_gronk in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-28-2017, 06:13 AM
  5. Add Property to UserForm via class module - please help
    By jaryszek in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-07-2016, 09:35 AM
  6. [SOLVED] Userform ComboBox Class Module not working
    By rodgersmg in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-24-2016, 03:23 AM
  7. [SOLVED] Userform & Dynamic Controls & Class Module
    By sarndt01 in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 09-18-2014, 01:53 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1