Declaration variable

radek225

Registered User.
Local time
Today, 05:41
Joined
Apr 4, 2013
Messages
307
How to declarate variable to working with in a form, subform and subform2
Form-->subform--->subform2

I tried to
Public ...
Private...
Dim....
and always variable working only in Form.
I don't want to declarate variable in a separate Module.
 
Like it or not, if you want to use a variable outside another form (does not matter if they are subforms of the main form; they are still different form). You need to declare them in a Standard module.
 
Hmm I have created forms with Public variables that are called from in subforms & other forms simply by using:

Code:
Form_frmExample.VariableName
 
Hmm I have created forms with Public variables that are called from in subforms & other forms simply by using:
True/Valid value only if the Form where the declarations are made is Active. The main difference between the variables declared in Form and Module is, when the Form is closed - all variables declared with it are flushed. In a module, the values are only taken off memory when the application is quit.

It is always best to keep Public - Public and Limited - Private. This is the main reason I try to discourage people writing Public sub's behind a Form, to be called by other form. If one procedure needs to be used in more than one place, move it to a Module. Hope this helps !
 
I see what you're talking about. You want to declare a variable in the Main form and be able to access it from the subform and subsubform? You can access the variable directly from the the subform but that's not good practice. Here's a better of accessing the variable from the subform:

Declare the variable in the main form as private:
Code:
Private strVar As String
Declare some properties to get and set it (these are your public accessors):
Code:
Property Get StrVariable() As String
    StrVariable = strVar
End Property

Property Let StrVariable(TheValue As String)
    strVar = TheValue
End Property
To set the variable's value from subform:
Code:
Me.Parent.StrVariable = "Hello World"
To set the variable's value from subsubform:
Code:
Me.Parent.Parent.StrVariable = "Hello World"
To get the variable's value from subform:
Code:
Msgbox Me.Parent.StrVariable
To get the variable's value from subsubform: ... well you get the gist.
 
Code:
Form_frmExample.VariableName
Avoid using the class name of the form i.e. "Form_frmExample", best to use the object itself, e.g. Forms!frmExample or Forms("frmExample") or when calling it from a subform use Me.Parent. Of course with Parent you won't get Intellisense but if you know the name of the object, method, field or property you can just type it in.
 
Avoid using the class name of the form i.e. "Form_frmExample", best to use the object itself, e.g. Forms!frmExample or Forms("frmExample") or when calling it from a subform use Me.Parent. Of course with Parent you won't get Intellisense but if you know the name of the object, method, field or property you can just type it in.

Good to know! Thanks
 
The main difference between the variables declared in Form and Module is, when the Form is closed - all variables declared with it are flushed.
Quite right Paul. It's always nice and neat to have everything in one place.

However, if you're accessing it from a subform, it's best that the variable no longer exists because the parent form (where the variable is declared) is now out of scope. That's why you either use Public properties or Classes.
 
However, if you're accessing it from a subform, it's best that the variable no longer exists because the parent form (where the variable is declared) is now out of scope. That's why you either use Public properties or Classes.
Agree 100% of it ;) Although the concept of Classes in VBA does elude this funny mind of mine. :D
 

Users who are viewing this thread

Back
Top Bottom