Get variable value in userform from Class module

jacko2401

New Member
Joined
Aug 24, 2011
Messages
35
I want to monitor when a value changes of several combo buttons on a user form. I have set this up using a class module to capture all the comboboxes on the userform. I have the following code in a class module called “ModComboClass”:
Code:
Public WithEvents ComboGroup As MSForms.ComboBox

Public Sub ComboGroup_Change()
        If strEditMode = "Yes" Then 'editing a fixture mode
            CmdCancel.Enabled = True
        End If

End Sub

In the class module I am monitoring when a combo box value in the user form is changed, check the strEditMode variable value and update the CmdCancel button if strEditMode = “Yes”. The strEditMode variable is stored in the user form and changes depending on changes completed in the form. This value changes correctly from tests completed within the user form. However, when I go through the code step by step in the class module, it never picks up the value of strEditMode from the userform. I have added a debug.print strEditMode line of code before the value of strEditMode is checked in the class module but it comes back with nothing as if it checks but cannot obtain the value. Are there any restrictions within the class module that stops it obtaining the value of the strEditMode variable in the user form? Any help will be greatly appreciated.

The code for the code module “Userform1” is:


Code:
Dim strEditMode As String 'Confirm if in edit mode
Dim cCombo As Controls
Dim ModCombos() As New ModComboClass
Dim ModCombosCount As Integer
 
Private Sub UserForm_Initialize()
For Each cCombo In Me.Controls
        If cCombo.Name Like "CboM_*" Then
            ModCombosCount = ModCombosCount + 1 ‘Add 1 to no of comboboxes count
            ReDim Preserve ModCombos(1 To ModCombosCount) ‘Reset ModCombo array
            Set ModCombos(ModCombosCount).ComboGroup = cCombo ‘Add combobox to ComboGroup in class module
        End If
Next cCombo
stEditMode = “No”

End Sub
 
Private Sub Cbo_Date_Change()
[Code added here for changes required within the form]
strEditMode = “Yes”

End Sub
 
Last question first: VBA is language and logic. Whilst certainly one may argue merits of a certain "methodology philosophy", I tend to stay away from declaring one way to be the "best". Andrew's suggestion of using a Public variable in a Standard Module is, as far as I am concerned, fine. If you start writing projects with a ton o' code in them, then overuse of Public variables can become nuisance causing.

Anyways, I'm only good for about another 20 minutes (it's very late here), but could you tell me where this control is? CmdCancel.Enabled = True

Just in reading your code, I don't see where the 'parent' form is to the combo box, so I'm not sure how you are changing the Enabled property.

Mark

A very good spot that I did correct earlier without updating here. When I tested before from Andrew's change to where the variable is declared, I did have to change this code to add the userform name so the line of code you code had to change to:

Code:
Userform1.CmdCancel.Enabled = "True"

My code and project is more complex than the cutdown example but my only issue was with this part of the code. Thanks for confirming about the risk with public declarations. I will keep this in mind in future. I try to keep variables declared in the sub procedures or the form/module it is required.
 
Upvote 0

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

Forum statistics

Threads
1,214,788
Messages
6,121,588
Members
449,039
Latest member
Arbind kumar

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