global or public variable

  • Thread starter Thread starter Wim
  • Start date Start date
W

Wim

Hi all,

When defining a variable in a form, that variable is most
of the time local, so only visible for that function.

I want however a variable, called from a module, and I
want it permanently visible for alle forms and reports.
How and where do you define a global or public variable
that is visible to all forms ?
Is it possible to pass a variable as an argument in a
forms docmd.open ?

Thanks !
 
Hmmm. I agree with the first part: declare a variable as Public in the
Declarations section of a Standard module, but I've never used the second
part unless I'm making the value of a Private variable in a Class module
available to the Public through a Get method.

Forms, by the way, are Class Modules, so if you want to keep the variable in
the Form's module, the Get property will work.

In the form's code, (assume the form is frmTest) do something like this:

dim strText as String

Public Property Get Test() As Variant
Test = strTest
End Property

Public Property Let Test(ByVal vNewValue As Variant)
strTest = CStr(vNewValue)
End Property

In a Standard module, create this Sub:

Public Sub TestIt()
Dim strTestIt As String

Forms!frmTest.Test = "test string"

strTestIt = Forms!criteria.Test

Debug.Print strTestIt

End Sub

The big difference between a form and a class module is that the form must
be open before you can use it's variables and methods. So you will want to
add code to test that the form is open before using it's variables.

Ted
 
Ted, the point of the function is that you cannot get the value of a
variable directly into text box, i.e. setting the Control Source of a text
box to
=MyVariable
won't work.
 
Back
Top