Object variable does not survive between procedures in a class module

Glory

Well-known Member
Joined
Mar 16, 2011
Messages
640
This class module has two procedures. One creates a checkbox, and the other interacts with the check box.

I'm having difficulty getting the second procedure in the class module to recognize the object variable created in the first (object or with block variable not set).

Can anyone help me out with this?
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
I can get a click event to work with the control's object variable, but a procedure will not.

There must be some way to get the class module to share the object variable, since the click event recognizes it.


Userform1 initialization code

Code:
private cCmd1 as cCmd
 
private sub userform1_initialize
 
set cCmd1 = New cCmd
 
cCmd.test1
 
end sub

Class module "cCmd" code

Code:
Private WithEvents cmd As MSForms.CommandButton
 
Public Sub test1()
 
[COLOR=blue]'Initialize multipage[/COLOR]
 
Set cmd = Userform1.Controls.Add("forms.CommandButton.1")
 
    With d01
 
        .Caption = "A"
        .Height = 24
        .Left = 5
        .Name = "cmd"
        .Top = 5
        .Width = 72
 
    End With
 
End Sub


Fails:
Code:
[COLOR=red]Public sub test2()[/COLOR]
 
[COLOR=red]cmd.caption = "B"[/COLOR]
 
[COLOR=red]End Sub[/COLOR]


Succeeds:
Code:
[COLOR=darkgreen]Private cmd_click()[/COLOR]
 
[COLOR=red]'test2[/COLOR]
 
[COLOR=darkgreen]cmd.caption = "C"[/COLOR]
 
[COLOR=darkgreen]End Sub[/COLOR]
 
Upvote 0
Code:
Dim CmdCollection As New Collection
 
[COLOR=black]'Initialize command button[/COLOR]
 
[COLOR=darkgreen]CmdCollection.Add cmd,"cmd"[/COLOR]
 
[COLOR=darkgreen]CmdCollection("cmd").Name[/COLOR]

Code:
test2()
 
[COLOR=red]CmdCollection("cmd").Name[/COLOR]
 
End Sub

I dimensioned a new collection and added the button to it...

The colletion method succeeds within the initialization procedure, and fails outside that scope. It doesn't matter if the object variable and collection are Public, and/or assigned their objects in a Static sub.

I cannot find any way to preserve an object variable for use between procedures in a class module. But the click event recognizes the variable just fine.
 
Upvote 0
This page seems to indicate that what I'm trying should be working.

"Module level variables declared in class modules exist for the lifetime of the class objects. Remember that with classes you cannot directly access code or data within the module without first creating an instance of an object defined by the class."

I've even tried instantiating a new version of the class within itself to ensure that the public variables remain in scope.
 
Upvote 0
Just tried using a class initialize procedure thinking maybe there were special rules that applied to variable retention within application events.

Did not work.
 
Upvote 0
If I declare the variable as a public variable in the module I use to call my userform, then it can be referenced in multiple procedures in a class module... except that I can't dimension the variable twice, which means that I can't use "WithEvents" to get two desirable traits up and running at the same time:

1) The control can be referenced by other controls
2) The control is linked to code

I have to choose between one or the other.

The only way I've found around this is to declare a public collection at the module-level in my "Userform Show" sub.

Is this really the only way forward...? Do I really have to have a public collection external to the class module to make this work? That's really indirect and backwards, if the whole point of using a class module in the first place is to get your code as self-contained as possible.
 
Upvote 0

Forum statistics

Threads
1,224,570
Messages
6,179,611
Members
452,931
Latest member
The Monk

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