Can an object variable survive between calls of multiple subs in a single class module?

Glory

Well-known Member
Joined
Mar 16, 2011
Messages
640
Here's the problem. I'm trying to get a checkbox created at runtime to adda new page to a multipage object, which is also created at runtime.

The object variable associated with the multipage object won't survive between calls of two separate subs in the class module I'm using. It appears as nothing in a later sub despite being set in an earlier sub.

This example uses a blank userform called "userform2" and a class module called "class2":

Userform2 object module code:

Code:
Public Mult, mp As Object
Private checker2, mp1 As Class2
Private Sub UserForm_Initialize()
    Dim checker1 As MSForms.CheckBox
    Set checker1 = Me.Controls.Add("forms.Checkbox.1")
    With checker1
        .Height = 22
        .Left = 6
        .Top = 12
        .Width = 72
        .Name = "CheckBox1"
        .Caption = "CheckBox1"
    End With
    Set checker2 = New Class2
    checker2.Init checker1
 
    Set mp = Me.Controls.Add("forms.Multipage.1")
    With mp
        .Height = 40
        .Left = 6
        .Top = 40
        .Width = 100
        .Pages.Remove (1)
    End With
    Set mp1 = New Class2
    mp1.Init2 mp
End Sub

Class2 module code:
Code:
Public Mult As Object
Private WithEvents cb As MSForms.CheckBox
Public Sub Init(ByVal CBox As MSForms.CheckBox)
    Set cb = CBox
End Sub
Public Static Sub Init2(ByVal Multi As MSForms.MultiPage)
    Set Mult = Multi
    If Not IsEmpty(Mult) Then: MsgBox Mult.Name
End Sub
Public Sub cb_Click()
    If cb.Value = True Then
        If Not Mult Is Nothing Then: MsgBox Mult.Name
    End If
End Sub

Please help me out with this.
 

Some videos you may like

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
7,929
Office Version
  1. 2016
Platform
  1. Windows
If I understand right, the peoblem is that you are creating 2 instances of Class2 but the Mult Object variable in your Class module is only initialised in the first Class instance hence not being seen in the second Class instance.

One solution is to amend the userform code as follows ( Edit in red)

Code:
Public Mult, mp As Object
Private checker2, mp1 As Class2
Private Sub UserForm_Initialize()
    Dim checker1 As MSForms.CheckBox
    Set checker1 = Me.Controls.Add("forms.Checkbox.1")
    With checker1
        .Height = 22
        .Left = 6
        .Top = 12
        .Width = 72
        .Name = "CheckBox1"
        .Caption = "CheckBox1"
    End With
    Set checker2 = New Class2
    checker2.Init checker1
 
    Set mp = Me.Controls.Add("forms.Multipage.1")
    With mp
        .Height = 40
        .Left = 6
        .Top = 40
        .Width = 100
        .Pages.Remove (1)
    End With
[COLOR=Red][B]'    Set mp1 = New Class2[/B][/COLOR]
    [B][COLOR=Red]checker2.Init2 mp[/COLOR][/B]
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,108,492
Messages
5,523,260
Members
409,506
Latest member
reneekeane

This Week's Hot Topics

Top