Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Class losing data just assigned to a variable

Status
Not open for further replies.

DougP

MIS
Dec 13, 1999
5,985
US
I am putting data into my class variables like so
I think when I Dim the Class Variables I am deleting original information or creating a new one.
Any way I want to store a Client name from one form and and the retrieve it later from another form.
Form1 stores info.
I think I'm missing something in the Class like creating an Array or Type

Code:
        Dim AClass As Class1
        Set AClass = New Class1
        AClass.Client = Me.Client  ' Client contains "Mr. Smith"

Form2 retrieves it
Code:
Private Sub Form_Activate()
    Dim BClass As Class1
    Set BClass = New Class1
    
    ' check to see if values are populated
    If Me.Client = "" Then
        Me.Client = BClass.Client  ' <<<< but its empty
    End If
End sub
here is my Class
Code:
Private pClient As String ' << I thought this would store the info but not sure what's going on?

Property Let Client(S As String)
    pClient = S
End Property

Property Get Client() As String
    Client = pClient
End Property

DougP, MCP, A+
 
Use a Public variable (once) ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
In the Declarations section of a standard code module:
Public myClass As Class1

In form1:
Set myClass = New Class1
myClass.Client = Me!Client

In form2:
Me!Client = myClass.Client

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Actually what you are doing is creating two instances of a class. Each instance is completely separate from the other.

You need to somehow pass the instance called "ClassA" to your form. One way is to make it a global variable that can be accessed from anywhere in your project, i.e.
Code:
Public AClass As Class1
But it's often not the best practice to create global variables, especially classes.
Another possible way is to add a property to the form that sets it's private instance of Class1. So the form code might be:
Code:
Private BClass As Class1

Public Property Set MyClass(CallerClass as Class1)
    'BClass is now referencing the original class, i.e.
    'they are one an the same
    Set BClass = CallerClass
End Property

Public Property Get MyClass() As Class1
    Set MyClass = BClass
End Property

In the code that calls the form:
Code:
    Dim AClass As Class1
    Set AClass = New Class1
    AClass.Client = Me.Client  ' Client contains "Mr. Smith"

    DoCmd.OpenForm "MyForm"
    Set Forms("MyForm").MyClass = AClass


 
I think you both hit on what I was missing
the Public declare.

DougP, MCP, A+
 
One last thing.
Do I need to free up the variables when I close my app?

Set "something" = nothing ???

DougP, MCP, A+
 
Set AClass = nothing

Probably best done in the UnLoad event of the form in which it was initialized.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top