Global variables disappear

T

Teewan

Hi guys

I have a training database, which I am still making alterations to and am
disturbed that sometimes while working on forms etc my Global variables are
suddenly all zero length strings.

On opening the database the system startup opens the Switchboard form.
The OnOpen event of the Switchboard form calls a number of functions located
in the SysConfiguration module which sets the Global variables to their
appropriate values. Every now and then those values suddenly revert to zero
lenght strings for no apparent reason.

It has been suggested to me that they are attached to the Swithchboard form
and I would loose them when I close the Switchboard. If this is the case how
and where should I be setting them from. Incidentally, I can't seem to make
them zero by closing the Switchboard.

I wondered if it is just a glitch occurring because I'm stopping and
starting bits of code etc. It doesn't seem to have happened (as yet) when
the database is being used by the average user working in there, only in my
testing versions. It is making me very nervous though as I use a Global
prefix for many of my Primary keys.

Thanks in advance
Teewan
 
A

Allen Browne

When testing your code, you often reset (Run | Reset, from code window), or
perform an operation that causes that effect. That destroys your public
variables.

While that is less likely in a production environment, it is an issue, and
it certainly makes it difficult to debug. When I first started in Access, I
created an extra public boolean variable, set it to True, tested it before
using any variable, and if not set, called the Init() function again.
Realistically, that's more trouble than it's worth, so now I don't use any
global variables at all. The only exception is where the built-in procedures
don't allow me to pass what I need, and so the lifetime that I need to rely
on is almost instantaneous.

You can use a hidden control on a form to hold a value that survives a
reset. For an extended lifetime (e.g. something that survives to the next
time you open the database again), you can write the value to a table. And
for your own functions, you can always pass the argument to the lower level
procedure.

HTH.
 
T

Teewan

Hi Allen,

While I'm not sure that was what I wanted to hear, thanks for the valuable
insight.

I have stored systems information in a table such as the path name for
linked tables the prefixes for various locations etc. On loading the
switchboard I was conducting look up for the location prefix and setting it
as a global. ie the database is used in various regional locations and I am
using the prefix to assist in management when the regional updates are moved
into the central one.

As I understand it your suggestion is that I include an invisible text box
on any form which might need the prefix and read it from there. I guess an
alternative is to call the setGlobalPrefix function on loading the form. Am I
on the right wave length here and is either option better than the other?

Thanks again, your a wealth of knowledge and I often utilise your website
and the answers you've given other people here.

Cheers
Teewan
 
A

Allen Browne

Yes, those are workable options.

The hidden text box would be my preference.

If you are using global variables, include the extra boolean that is set to
True when you initialize. You can then text it before you use any global,
and re-initialize if necessary, e.g.:
If Not bInit Then
Call setGlobalPrefix()
End If
'code here that uses a global variable.

In general, I hate globals because VBA permits a local with the same name,
and that's hard to debug. Another alternative is to create a public function
with a static variable that automatically re-initializes itself when needed.
This kind of thing:

Public Function GetGlobalPrefix()
Static varPrefix as Variant
If IsEmpty(varPrefix) Then
'whatever code initializes it.
End If
GetGlobalPrefix = varPrefix
End Function
 
C

Chriske911

Hi guys
I have a training database, which I am still making alterations to and am
disturbed that sometimes while working on forms etc my Global variables are
suddenly all zero length strings.

On opening the database the system startup opens the Switchboard form.
The OnOpen event of the Switchboard form calls a number of functions located
in the SysConfiguration module which sets the Global variables to their
appropriate values. Every now and then those values suddenly revert to zero
lenght strings for no apparent reason.

It has been suggested to me that they are attached to the Swithchboard form
and I would loose them when I close the Switchboard. If this is the case how
and where should I be setting them from. Incidentally, I can't seem to make
them zero by closing the Switchboard.

I wondered if it is just a glitch occurring because I'm stopping and
starting bits of code etc. It doesn't seem to have happened (as yet) when
the database is being used by the average user working in there, only in my
testing versions. It is making me very nervous though as I use a Global
prefix for many of my Primary keys.

Thanks in advance
Teewan

most of the time I use a form that never closes
be it a kind of switchboard or any other start up form
I also use this to keep an open connection to the backend

indeed, during testing the global vars do tend to lose their values
but I have never had a problem with it in a live user environment
I do use error trapping everywhere to avoid it anyhoe :D

if isnull(globalvar) or globalvar = "" then .....
and you can put this all in a module as to retrieve them if needed

grtz
 
T

Teewan

Hi guys,

Thanks for the help. This is basically the way I've gone. My switchboard is
open all the time and I have included error checking wherever the globals are
used. And hoping I've found them all. Am on a real tight timeline now.

Cheers
 

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