Thank you for having a read:
I have two forms with dynamically created comboboxes attempting to reference the same class module.
The first works as expected with the relevant code as follows:
Code
Sub Reactions()
Dim clsComboBox As New cComboBox
Dim MyCBx As MSForms.ComboBox
Set m_colComboBoxEvents = New Collection
loop starts here
'other code
If MyFRMS < CInt(MyFill(UBound(MyFill))) Then
Set MyCBx = Me.Frame20.Controls.Add("Forms.ComboBox.1", "MyNCBX" & MyFRMS, True) 'adds combobox
With MyCBx
.Top = 330
.Left = 90 + (160 * (MyFRMS - 1))
.Width = 160
.Height = 20
.FontSize = 8
.FontName = "Times New Roman"
.ColumnCount = 2
.ColumnWidths = ",0"
.Value = "========================>"
End With
Set clsComboBox = New cComboBox
Set clsComboBox.Box = MyCBx
m_colComboBoxEvents.Add clsComboBox, CStr(m_colComboBoxEvents.Count + 1)
'other code
loop ends here
End Sub
Display More
Class module code here:
Code
Option Explicit
Private WithEvents m_ComboBoxEvents As MSForms.ComboBox
Public Property Set Box(RHS As MSForms.ComboBox)
Set m_ComboBoxEvents = RHS
End Property
Private Sub m_ComboBoxEvents_Change()
If REACTIONS.Visible = True Then
Debug.Print "hELlO"
'\\ WHEN COMBOBOX CHANGES ON REACTION FORM IT PASSES THE CAPTION INFORMATION FOR THE BUTTONS.
If Len(Box.Name) = 7 Then REACTIONS.Frame20.Controls("MyNCBtn" & Right(Box.Name, 1)).Caption = Box.List(Box.ListIndex, 1) ' will only work until 9
If Len(Box.Name) = 8 Then REACTIONS.Frame20.Controls("MyNCBtn" & Right(Box.Name, 2)).Caption = Box.List(Box.ListIndex, 1) ' will work until 99
End If
Debug.Print "Hi"
If UFSTDRDS.Visible = True Then
MsgBox ("Hello")
End If
End Sub
Public Property Get Box() As MSForms.ComboBox
Set Box = m_ComboBoxEvents
End Property
Display More
On another form in the same workbook I am attempting to achieve a similar result using:
Code
Sub Not_Working()
Dim clsComboBox As New cComboBox
Dim MyCBx As MSForms.ComboBox
Dim m_colComboBoxEvents As Collection ' If I remove this line I get variable undefined error in next line, but it is not required in the working code above
Set m_colComboBoxEvents = New Collection
Loop starts here
Set MyCBx = Me.MultiPage1.Pages(2).Controls.Add("Forms.ComboBox.1", "MyNCBox" & x, 1)
Dim MyCBxfill As Variant
MyCBxfill = ws1.Range("A1").CurrentRegion
With MyCBx
.Top = 280 + ((x - 1) * 30)
.Left = 250
.Width = 350
.Height = 20
.FontSize = 8
.FontName = "Times New Roman"
.ColumnCount = UBound(MyCBxfill, 2)
'.ColumnWidths = ",0"
.List = MyCBxfill
End With
Set clsComboBox = New cComboBox
Set clsComboBox.Box = MyCBx
m_colComboBoxEvents.Add clsComboBox, CStr(m_colComboBoxEvents.Count + 1)
Loop ends here
End Sub
Display More
The reactions code as seen in the class module "if reactions.visible" works fine and when I change those comboboxes the code in the if statement of the class module works and I get the debug.print of both "HeLio" and "Hi"... however nothing from the class code runs when I change the comboboxes on the second form using the not working code.
Any insight and improvement to the existing code would be appreciated.
Regards
Justin