Add Dynamic UserForm Controls on Multipage to Class

MrMegadeth

New Member
Joined
Jul 30, 2014
Messages
5
My apologies as I know this has been asked many times but I cannot get this to work for my UserForm. I have highlighted the code in question in blue.

This is my class (BOL_BOXES) that will fire for the dynamically created comboboxes:

Code:
Public WithEvents btn As MSForms.ComboBox

Private Sub btn_Change()
Debug.Print "Change Working"
End Sub

Private Sub btn_Click()
Debug.Print " ClickWorking"
End Sub

Private Sub btn_DropButt*******()
Debug.Print "Working"
End Sub

This is the UserForm Code - Parsed a bit due to length

Code:
Private Sub UserForm_Initialize()
Dim BOLAmount As Integer
Dim pComboBoxes As Collection
Dim BOLCBox As BOL_Boxes
Dim cCtrl As MSForms.Control

Set pComboBoxes = New Collection
...
    'add required controls to each page
    For q = 1 To 12
        TopNum = 6 + (18 * q) + 8 * q
         Set cCtrl = Me.MultiPage1.Pages(i - 1).Controls.Add("Forms.combobox.1", "Chief" & q)
            With cCtrl
                .Left = 12
                .Top = TopNum
                .Width = 96
                .RowSource = ("SandList" & i)
            End With
[COLOR=#0000ff]        Set cCtrl = Me.MultiPage1.Pages(i - 1).Controls.Add("Forms.combobox.1")
            With cCtrl
                .Left = 120
                .Top = TopNum
                .Width = 96
                .Name = ("BOL" & q)
             ' based on my understanding this should create a collection and then add the new control cCtrl to the class
                Set BOLCBox = New BOL_Boxes
                Set BOLCBox.btn = cCtrl
                pComboBoxes.Add BOLCBox[/COLOR]
            End With
        Set cCtrl = Me.MultiPage1.Pages(i - 1).Controls.Add("Forms.textbox.1", "Used" & q)
            With cCtrl
                .Left = 228
                .Top = TopNum
                .Width = 96
                .TextAlign = fmTextAlignRight
            End With
    Next q

The code compiles fine and the userform looks correct but I cannot get the class to fire. I am missing something since the Control is located on a multipage?
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Try to adopt the following code, which simply adds 3 ComboBoxes to the UserForm at runtime...

Class Module:

Code:
[COLOR=darkblue]Option[/COLOR] [COLOR=darkblue]Explicit[/COLOR]

[COLOR=darkblue]Public[/COLOR] [COLOR=darkblue]WithEvents[/COLOR] CBox [COLOR=darkblue]As[/COLOR] MSForms.ComboBox

[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] CBox_Change()
    Debug.Print "Change working..."
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] CBox_Click()
    Debug.Print "Click working..."
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] CBox_DropButt*******()
    Debug.Print "DropButt******* working..."
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

UserForm module:

Code:
[COLOR=darkblue]Option[/COLOR] [COLOR=darkblue]Explicit[/COLOR]

[COLOR=darkblue]Dim[/COLOR] aCBoxes() [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]New[/COLOR] BOL_Boxes

[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] UserForm_Initialize()

    [COLOR=darkblue]Dim[/COLOR] myCBox      [COLOR=darkblue]As[/COLOR] MSForms.ComboBox
    [COLOR=darkblue]Dim[/COLOR] LeftPos     [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Double[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] TopPos      [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Double[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] Gap         [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Double[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] i           [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    
    Me.Width = 240
    Me.Height = 160
    
    LeftPos = 12
    TopPos = 18
    Gap = 20
    
    [COLOR=darkblue]ReDim[/COLOR] aCBoxes(1 [COLOR=darkblue]To[/COLOR] 3)
    
    [COLOR=darkblue]For[/COLOR] i = 1 [COLOR=darkblue]To[/COLOR] 3
        [COLOR=darkblue]Set[/COLOR] myCBox = Me.Controls.Add("Forms.ComboBox.1", "ComboBox" & i, [COLOR=darkblue]True[/COLOR])
        [COLOR=darkblue]With[/COLOR] myCBox
            .Left = LeftPos
            .Top = TopPos
            [COLOR=green]'etc.[/COLOR]
        [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
        [COLOR=darkblue]Set[/COLOR] aCBoxes(i).CBox = myCBox
        TopPos = TopPos + myCBox.Height + Gap
    [COLOR=darkblue]Next[/COLOR] i
    
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

Hope this helps!
 
Upvote 0
I was able to get it in the end. I modified the code a bit here and there. I don't think I was returning my collection to the class correctly.
 
Upvote 0

Forum statistics

Threads
1,216,373
Messages
6,130,228
Members
449,567
Latest member
ashsweety

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