Global variables and memory

L

Lars Brownie

I'm planning to redesign speed up an application by cutting out a lot of
duplicate Dlookups (in fact Allen's Elookups) and performing this only once
for every value during startup of the app. I'm planning of storing 8 often
used lookup values (department name, username, path of app, etc) in global
variables. I've read that global variables can eat up a lot of memory. Would
this be the case if I use these 8 global stringvars or would that be just
peanuts?

Thanks, Lars
 
K

Klatuu

Memory usage is not really an issue, but using Global variables is. I
recommend they never be used. They are unreliable. For example, any
unhandled error in your app will reset all the Global variables.
My preferred way to do this is with Public functions that use Static
variables to store the values. If you pass a value to it, it retains the
passed value until a new value is passed or the application is closed. If
you call it with no argument, it returns the last value it received.

Here is an example to return a string value:

Public Function FooBah(Optional NewValue As Variant) As String
Static strOldValue as String

If Not IsMissing(NewValue) Then
strOldValue = NewValue
End If
FooBah = strOldValue
End Function

So to use it in your case, you would do something like:

FooBah(DLookup("[SomeField]", "[SomeTable]"))

The when you need the value anywhere in your app:

strWinkie = FooBah
 
D

Dirk Goldgar

Lars Brownie said:
I'm planning to redesign speed up an application by cutting out a lot of
duplicate Dlookups (in fact Allen's Elookups) and performing this only
once for every value during startup of the app. I'm planning of storing 8
often used lookup values (department name, username, path of app, etc) in
global variables. I've read that global variables can eat up a lot of
memory. Would this be the case if I use these 8 global stringvars or would
that be just peanuts?


Peanuts.

But be aware that any unhandled error can cause your global variables to be
reset. I usually wrap these in functions (or Property Get procedures) that
check whether the global variable is Empty/Null/Nothing and reloads it if
so.
 
L

Lars Brownie

Thanks Dave, Dirk. This really saved me!

Klatuu said:
Memory usage is not really an issue, but using Global variables is. I
recommend they never be used. They are unreliable. For example, any
unhandled error in your app will reset all the Global variables.
My preferred way to do this is with Public functions that use Static
variables to store the values. If you pass a value to it, it retains the
passed value until a new value is passed or the application is closed. If
you call it with no argument, it returns the last value it received.

Here is an example to return a string value:

Public Function FooBah(Optional NewValue As Variant) As String
Static strOldValue as String

If Not IsMissing(NewValue) Then
strOldValue = NewValue
End If
FooBah = strOldValue
End Function

So to use it in your case, you would do something like:

FooBah(DLookup("[SomeField]", "[SomeTable]"))

The when you need the value anywhere in your app:

strWinkie = FooBah

--
Dave Hargis, Microsoft Access MVP


Lars Brownie said:
I'm planning to redesign speed up an application by cutting out a lot of
duplicate Dlookups (in fact Allen's Elookups) and performing this only
once
for every value during startup of the app. I'm planning of storing 8
often
used lookup values (department name, username, path of app, etc) in
global
variables. I've read that global variables can eat up a lot of memory.
Would
this be the case if I use these 8 global stringvars or would that be just
peanuts?

Thanks, Lars
 
A

Armen Stein

But be aware that any unhandled error can cause your global variables to be
reset. I usually wrap these in functions (or Property Get procedures) that
check whether the global variable is Empty/Null/Nothing and reloads it if
so.

Yup, good idea.

Another way to clear Globals unintentionally - cancel the Unload event
of your main always-open form in order to prevent the user from
inadvertently closing your app using the X button in the Access
window. It all works and your app stays open, but the Globals are
cleared.

Another alternative to Static variables in functions - a hidden form
that holds the "global" values in text controls. Advantage: during
development they can be seen and even modified by merely making the
form visible.

And in Access 2007, we now have the TempVars collection, which can be
used for global values.

Armen Stein
Microsoft Access MVP
www.JStreetTech.com
 
T

Tony Toews [MVP]

Armen Stein said:
Another alternative to Static variables in functions - a hidden form
that holds the "global" values in text controls. Advantage: during
development they can be seen and even modified by merely making the
form visible.

I've been using this technique for at least ten years.

Tony
--
Tony Toews, Microsoft Access MVP
Please respond only in the newsgroups so that others can
read the entire thread of messages.
Microsoft Access Links, Hints, Tips & Accounting Systems at
http://www.granite.ab.ca/accsmstr.htm
Tony's Microsoft Access Blog - http://msmvps.com/blogs/access/
 

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