Public variables become empty???

B

Bill

I have an application that includes installation properties
from which public variables are loaded when the application
starts. They include such things as file paths, and a variety of
names that are peculiar to the installation. Like "YOUR
COMPANY NAME HERE" that gets plugged into
label box captions in form headings and reports plus other
places special to the installation. All of the public variables,
all of which are defined within a general module, are
essentially static and "read-only" throughout a normal user
session.

During some testing of some un-related functions, those
public variables became empty, i.e., "". At one of the points
where that became apparent, I inserted a MsgBox statement
to print out the current values of a few of them only to discover
they were in fact empty.

What could cause public variables to suddenly become empty?

Thanks,
Bill
 
A

Allen Browne

During development, you probably test your code and when problems occur, you
choose Reset on the Run menu (in the code window). This resets the variables
also.

Of course, the variables do not survive once you have closed the program, so
the best idea might be to write them to a table, and then read them from
there as needed instead of using public variables.

If you do not need them to survive between sessions, you might leave a form
open and hidden, and read the values from the form when needed. Typically
this would be the form where the user enters the original values.
 
B

Bill

Allen,
The public variables are defined in a module that also
contained a couple of one-line functions used to pass
arguments to reports in O2k. It may very well be that
those variables were corrupted the first time one of
those functions was executed during some of my other
testing. I've split that module into two separate modules
with all the public variables so that they are not in a
module subjected to open/close.

The values involved here are indeed stored in a single
table record. When the application starts, that record
is read and the public variables are set accordingly.

I'll continue with development activity and keep a watch
for any further "corruptions".

Thanks,
Bill
 
A

Allen Browne

Many years ago I experimented with using global variables for this kind of
thing, and found it awkward do debug. Added another public boolean to
indicate if the variables are currently initialized:
Public gbInitialized As Boolean
Function Init()
'set your variables here
gbInitialized = True
End If

Then test this one, and reinitialize if necessary before using the others:
If Not gbInitialized Then
Call Init()
End If
'use your variable

Eventually gave up on using public variables like this, finding it was
easier to just grab the value when needed.
 
B

Bill

What method did you use to "just grab the value when needed"?

The references to the public variables in my application are
frequent and many. For that reason, I didn't even consider
anything that might be somewhat involved each time I needed
to make use of those values. However, I'm always willing to
learn better ways of doing things.

Thanks,
Bill
 
A

Allen Browne

DLookup() where appropriate, or ELookup() which is about twice as fast, but
still slow if you have lots of lookups or it is in a loop. The extended
DLookup() is at:
http://allenbrowne.com/ser-42.html

If you need lots of values from a table at once, and performance is an
issue, opening one SQL statement to get them all will be much more
efficient.

If the lookups are very frequent, I use controls on a hidden form to hold
the values, though personally I don't do this often. You may already have a
hidden form open to simulate an application close event, or to run an
application wide timer, or to hold info about the user who logged in.
 
B

Bill

"opening one SQL statement to get them all" is how I do it now.
Having isolated ALL the global variables into a general module,
not subjected to the open/close sequence, seems to be working
okay. If I have any further difficulties, I'll store them in a hidden
form and abandon the use of the public variable approach.

Thanks for your time and help.
Bill
 

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