changing public variables from within a private function

Fantast

Registered User.
Local time
Today, 16:18
Joined
Dec 13, 2011
Messages
41
Hi. This question is not only to solve a problem for me, but also to understand some of the VBA logic.

In one of my forms I declare a variable as a class (which I have written myself). After that I set some of it's properties in one of my private functions. However, when trying to retrieve those properties from an another private sub, the property values are gone. Changing the initial function from private to public solves the issue, but that doesn't seem to me as the correct way of handling the issue.

Is there a way to change the properties of a public class variable from within a private function, and have those changes being applied publicly instead of only locally?

Thanks in advance.
 
The following is a representation of my code:

Code:
Option Compare Database
Option Explicit

Public clsClass As New CClass

Public Function InitiatePublicVars()
    clsClass.Name = "Name"
    clsClass.Source = "Source"
End Function

Private Sub Form_Open(Cancel As Integer)
    InitiatePublicVars
End Sub

Private Sub TestSub()
    Debug.Print clsClass.Name
End Sub

Public Sub select_customer_Click()
    TestSub
End Sub

When the form is opened, Open_form() will call InitiatePublicVars(). However, whatever properties are set in InitiatePublicVars(), are gone outside Open_form(). TestSub() will show no result. If I change Open_form() from private to public, TestSub() will give "Name".
 
I don't see anything particularly wrong with your code. Public or Private should work.

Have you Debug > Compiled since?
 
Aye, Now that I try it again it seems to be working correctly... I thought I had tested it properly, but apparently I had not. My appolopies for wasting your time.
 

Users who are viewing this thread

Back
Top Bottom