Losing global variables

G

Guest

In debug mode I understand that during a error, if you hit reset you can lose
all your global and module variables. To quote from the help info, reset
does the following:
"Clears the Call stack and clears the module level variables."

However, in production mode, If you do not overlay the values in the global
variable, How can you lose them. I may case the strings become blank.

Thanks
 
B

Baz

He didn't mention an MDE, and he is obviously experiencing this problem, so
it's a pound-to-a-penny that unhandled errors are behind it.
 
G

Guest

Static function variables are not affected by this problem. Instead of a
global variable, you may want to consider trying one out. Put the function
in a standard module, like you would a global varialbe:

Public Function MyVar(Optional ByVal varNewVal As Variant) As Variant
Static varOldVal As Variant

If Not IsMissing(varNewVal) Then
varOldVal = varNewVal
End If
MyVar = varOldVal
End Function
 
B

Baz

Neat! I'd never have thought of that...

Klatuu said:
Static function variables are not affected by this problem. Instead of a
global variable, you may want to consider trying one out. Put the function
in a standard module, like you would a global varialbe:

Public Function MyVar(Optional ByVal varNewVal As Variant) As Variant
Static varOldVal As Variant

If Not IsMissing(varNewVal) Then
varOldVal = varNewVal
End If
MyVar = varOldVal
End Function
 
G

Guest

I use this instead of global variables. I guess I forgot to tell the OP how
to use it.
If you call it with a value, it will replace the existing value with the
value received. If you call it with no value, it returns the existing value
in the static variable.
 
G

Guest

I tried this. I forced an unhandled exception and the static variable ALSO
got wiped out.
 
G

Guest

Interesting. Was it in a standard module. I have tried to make it loose
value by forcing errors and have not been able to do so.
What error did you force?
 
S

Stefan Hoffmann

hi,
Interesting. Was it in a standard module. I have tried to make it loose
value by forcing errors and have not been able to do so.
What error did you force?
I have tested it last week with A2k3; when raising a DivByZero error in
an event procedure, all modules are cleared.

I'm not sure, but afaik under A97 only variables in the module raising
the error where deleted.


mfG
--> stefan <--
 
G

Guest

Yeah, hard to remember. Last time I worked with 97 was in 99.
So far, 2003 is my favorite version.
 
G

Guest

I commented out a line which fills in a required field into a table. I also
took out my on error goto statement so I would get the unhandled exception.
Then when I checked the statis value it was blank. It displayed as "", not
at Null.

I am using Access 2000 if that is omportant.

I placed the code in a module called Globals. That is my my only global
type module. All other modules are tied to forms or report.
 
G

Guest

What data type does the function where the static varialbe is return?
What value did it have before you forced the error?
 
G

Guest

I tried it with variant ( as suggested) also with string, as all my globals
are strings.
I tried it a user name field which gets filled in at login time. It is a
string (for my testing it had 7 characters - all alpha, no special chars, no
numerics). After the first call to the function I checked the value of the
static ariable and it was correct.
 
G

Guest

I am going to have to rerun some test, then. I have not been able to make it
loose value.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top