Need to link up controls on a userform created at runtime with code

Glory

Well-known Member
Joined
Mar 16, 2011
Messages
640
It’s been suggested to me that I use a class module to accomplish this. It makes sense given what I’ve been reading about class modules. But I can’t get off the ground.
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p> </o:p>
Do I start by creating a sub that will act as a method in the class module, which will create new controls on the userform I’m working with?
<o:p> </o:p>
I can’t figure out how to call the sub in the class module. “Call ClassModuleName.ProcedureName” isn’t working?
<o:p> </o:p>
Can anyone offer me any help?
 
Both variables have to be passed to that single initialization procedure as separate arguments? I never would have gotten the answer, thank you, thank you.
 
Upvote 0

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
I only just discovered each "WithEvents" statement has to be on a separate line in the module-level declarations, that broke some click events for a while.

I've added a checkbox to the multipage, but now the click event doesn't trigger. I'm still working with it, but I'm not sure what's going wrong.

Code:
'Userform2 initialization
Private mp As Object
Private checker1, mp1 As Class2
Private Sub UserForm_Initialize()
    Dim theClass1 As New Class2
    Dim checker1 As MSForms.CheckBox
    Dim mp As MSForms.MultiPage
    Set mp = Me.Controls.Add("forms.Multipage.1")
    With mp
        .Height = 120
        .Left = 6
        .Top = 6
        .Width = 162
        .Pages.Remove (1)
        .Pages(0).Controls.Add ("forms.Checkbox.1")
    End With
    With mp.Pages(0).Controls(0)
        .Height = 22
        .Left = 6
        .Top = 12
        .Width = 72
        .Name = "CheckBox1"
        .Caption = "CheckBox1"
    End With
    Set checker1 = mp.Pages(0).Controls(0)
    theClass1.Init checker1, mp
End Sub

Code:
'Class2 module code
Private WithEvents cb As MSForms.CheckBox
Private WithEvents Mult As MSForms.MultiPage
Private WithEvents Mult2 As MSForms.MultiPage
Public Sub Init(ByVal CBox As MSForms.CheckBox, Multi As MSForms.MultiPage)
    Set cb = CBox
    Set Mult = Multi
End Sub
Public Sub cb_Click()
If cb.Value = True Then
    Mult.Pages.Add
End If
End Sub
 
Upvote 0
I pasted an old version of the code by mistake with some issues in it I had already worked out. Here's the current version.

Code:
[COLOR=seagreen]'Userform object module[/COLOR]
 
Private mp As Object
Private checker1, mp1 As Class2
Private Sub UserForm_Initialize()
    Dim theClass1 As New Class2
    Dim checker1 As MSForms.CheckBox
    Dim mp As MSForms.MultiPage
    Set mp = Me.Controls.Add("forms.Multipage.1")
    With mp
        .Height = 120
        .Left = 6
        .Top = 6
        .Width = 162
        .Pages.Remove (1)
        .Pages(0).Controls.Add ("forms.Checkbox.1")
    End With
    With mp.Pages(0).Controls(0)
        .Height = 22
        .Left = 6
        .Top = 12
        .Width = 72
        .Name = "CheckBox1"
        .Caption = "CheckBox1"
    End With
    Set checker1 = mp.Pages(0).Controls(0)
    theClass2.Init checker1, mp
End Sub

Code:
[COLOR=seagreen]'Class2 module[/COLOR]
 
Private WithEvents cb As MSForms.CheckBox
Private WithEvents mult As MSForms.MultiPage
Public Sub Init(ByVal CBox As MSForms.CheckBox, Multi As MSForms.MultiPage)
    Set cb = CBox
    Set mult = Multi
End Sub
Public Sub cb_Click()
If cb.Value = True Then
    MsgBox "Hey"
    'Mult.Pages.Add
End If
End Sub

I think the source of the problem might have something to do with the control "CheckBox1" being declared as type "MSForms.Checkbox" while it is simultaneously a member of a controls collection for a different object...?

Whatever it is, it's stopped me cold.
 
Upvote 0
Never mind, I think I got it. Got the set and declaration mixed up for the class instantiation, among other things.
 
Upvote 0
Code:
Private WithEvents cb As MSForms.CheckBox
Private WithEvents mult As MSForms.MultiPage
Public Sub Init(ByVal CBox As MSForms.CheckBox, Multi As MSForms.MultiPage)
    Set cb = CBox
    Set mult = Multi
End Sub
Public Sub cb_Click()
If cb.Value = True Then
    mult.Pages.Add
End If
End Sub

Code:
Private theClass As Class2
Private mp As MSForms.MultiPage
Private a As MSForms.CheckBox
Private Sub UserForm_Initialize()
    Set mp = Me.Controls.Add("forms.Multipage.1")
    With mp
        .Height = 120
        .Left = 6
        .Top = 6
        .Width = 162
        .Pages.Remove (1)
    End With
    Set a = mp.Pages(0).Controls.Add("forms.Checkbox.1")
    With a
        .Height = 22
        .Left = 6
        .Top = 6
        .Width = 72
        .Name = "CheckBox1"
        .Caption = "CheckBox1"
    End With
    Set theClass = New Class2
    theClass.Init a, mp
End Sub

Ignore everything I wrote between your last post and this one, except the gratitude. Without your example I never would have worked this through.
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,391
Members
449,080
Latest member
Armadillos

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top