Public Variable losing its value

  • Thread starter Thread starter mattmanp
  • Start date Start date
M

mattmanp

I have a public variable set up so that I close one form, enter dat
and then go back to the original, and be able to load the sort an
filter from previously, but I have to close the form, not minimiz
it. I can set the values fine right before the DoCmd.Close, but in m
OnLoad function they come up as being empty. Could this be because o
context

Also, how can I use a variable as a DefaultValue in a textbox? Thank
for the help
 
Public variables only exist while the module they are in is running. As
soon as you close the form where the variable is declared, it is gone. Here
is a function I use instead of public variables. Put this in a modlue:

Function MyPubVar(Optional varPubVal As Variant) As Variant
'Returns current value if no parameter passed
'Stores value passed
'Establishes a default value for first call

Static varSaveVal As Variant

If IsMissing(varPubVal) Then
If IsEmpty(varSaveVal) Then
varSaveVal = "GotIt!" <--Your defalut value goes here
End If
Else
varSaveVal = varPubVal
End If
MyPubVar = varSaveVal
End Function

To use it, if you want to know the current value:

CurVal = MyPubVar() - The current value will be returned

CurVal = MyPubVar("Foo") - Assigns and returns the value "Foo". "Foo" will
be returned as long as your database is open and until the value is reset.

The second part of your question is no, a variable will not work. That is
part of the reason to use the function above, because you can use a function
as a default value.
 
mattmanp said:
I have a public variable set up so that I close one form, enter data
and then go back to the original, and be able to load the sort and
filter from previously, but I have to close the form, not minimize
it. I can set the values fine right before the DoCmd.Close, but in my
OnLoad function they come up as being empty. Could this be because of
context?

Also, how can I use a variable as a DefaultValue in a textbox? Thanks
for the help!


Note that a class module and a standard module are quite
different things.

Public variables in a class module (including Form/report
modules) are properties of the class and only exist when the
class is instantiated (or opened).

OTOH, Public variables in a standard module are available to
all VBA code anywhere in your application. They will lose
their value whenever the project is reset, which will happen
whenever an unhandled error occurs. These variables are not
available outside VBA code (i.e. in form/report properties
and queries).

Since a Public Function is available everywhere in Access,
you can use one to retrieve the value of a public variable:

Public MyVar As <whatever>
Public Function GetMyVar() As <whatever>
GetMyVar = MyVar
End Function

An alternative to using global variables that avoids all
those issues is to use text boxes on an invisible form that
remains open while your application is open.
 
Thank you M Barton.

That was the tiny little clue I needed to fix mine and educate me of the
difference...
 
Now, that is good to hear. I love it when someone finds the
answer to their question by digging through old posts.

A general word to the wise, Google groups is a veritable
fount of knowledge.
 
Back
Top