Form-Level Variable Values Go "Poof"?

P

PeteCresswell

In many of my forms, I maintain a form-level Long variable called
"mScreenMode" - which gets set to things like 1=Browse, 2=Edit, 3=Add.

Typically there will be a command button on the screen whose .Caption
varies between "Change", "Save", and "Add" depending on screen mode.

As part of that scheme, I'm often casing out on mScreen mode.

This works 99.9% of the time.

But every so often a Case statement will drop down to Else because
mScreenMode has somehow become zero.

This happens much more often in development mode - and I would expect
it there bco things I'm doing like stepping through code and
interrupting it.

But in production mode, it shouldn't happen unless I've got one or
more bugs in my code.

But it happens so rarely in production that I've got to wonder if MS
Access is somehow capable of losing form-level variable values. i.e.
if it were a bug, users would provoke it with some regularity.

Comments?
 
T

Tom van Stiphout

On Fri, 2 Oct 2009 07:08:46 -0700 (PDT), PeteCresswell

I am assuming per best practices you are distributing MDE or ACCDE to
your clients. Then the code cannot be reset like it can when you're
working as a developer.
To further debug, you could tick a timer every second, and if
mScreenMode=0, put up a messagebox and ask user what they were doing
just now.

-Tom.
Microsoft Access MVP
 
K

Klatuu

Global variables will reset whenever there is an unhandled error in your
code. As Tom said, this occurs frequently in development mode. There is a
way to avoid that problem. Rather than using a module level variable, use a
Static function to hold the value. A Static function will retain its current
value between calls.

This example will set the value to be returned when you pass it a value, but
will always return the current value:

Public Function ScreenMode(NewValue As Variant) As Long
Dim lngOldValue As Long

If Not IsMissing(NewValue) Then
lngOldValue = CLng(NewValue)
End If
ScreenMode = lngOldValue
End Function

So all you do is use the function instead of directly assigning the value.
It will not be affected by a code error because the variable being used
(lngOldValue) is a local variable.
 
P

PeteCresswell

 Rather than using a module level variable, use a
Static function to hold the value.  A Static function will retain its current
value between calls.

That sounds like the ticket.

Thanks!
 
P

(PeteCresswell)

Per Klatuu:
Public Function ScreenMode(NewValue As Variant) As Long
Dim lngOldValue As Long

If Not IsMissing(NewValue) Then
lngOldValue = CLng(NewValue)
End If
ScreenMode = lngOldValue
End Function

One question: why not just define the variable as Static and
continue to access it directly?

e.g.

OLD: Dim mScreenMode As Long
NEW: Static mScreenMode As Long
 

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