473,735 Members | 2,082 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Many global vars in an A97 app - good or bad? Why?

MLH
I've read some posts indicating that having tons of GV's in
an Access app is a bad idea. Personally, I love GVs and I
use them (possibly abuse them) all the time for everything
imaginable - have been for years. If the machine has memory
to spare and windows can use it - I'm thinking "Why not?"

I was wondering what some of you have to say about that,
particularly any severe "gotchas" you've had the unfortunate
experience to contend with.
Nov 13 '05 #1
33 3046
On Tue, 08 Nov 2005 11:24:15 -0500, MLH <CR**@NorthStat e.net> wrote:
I've read some posts indicating that having tons of GV's in
an Access app is a bad idea. Personally, I love GVs and I
use them (possibly abuse them) all the time for everything
imaginable - have been for years. If the machine has memory
to spare and windows can use it - I'm thinking "Why not?"

I was wondering what some of you have to say about that,
particularly any severe "gotchas" you've had the unfortunate
experience to contend with.


The problem is that global variables tightly couple together code that does
different things at different times, and since any code can update the
variable at any time, you can never be sure it was left in the state you think
it was. Generally, it's best if you can arrange for each piece of code to
send data directly to the thing that will use it.

One abuse of globabl vairables I've seen frequently is to keep track of the
last active control. The trouble with this is, what happens when you toggle
the focus between forms? The right place for this variable is in the form's
module, where there is no confusion as to what form's state we're talking
about.
Nov 13 '05 #2
I use GV in all my VB6 programs. I take paper schematics and put them
into electronic format and put intelligence into them using CGM
intelligence graphics. I create a history list of an index selection
that is held by GV. I say use them if you need to but don't abuse them
if you dont have to.

Nov 13 '05 #3
MLH
What about these 3: GlobalString, MySQL and GlobalVariant. I use them
really for convenience more than anything. Anytime I want to concat a
bunch of crap like

MySQL = "SELECT tblNCcounties.C ounty, tblNCcities.Cit yName
FROM (tblNCcities LEFT JOIN tblNCcityCounty JunctionTable "
MySQL = MySQL & "ON tblNCcities.Cit yID =
tblNCcityCounty JunctionTable.C ityID) LEFT JOIN tblNCcounties ON "
MySQL = MySQL & "tblNCcityCount yJunctionTable. CountyID =
tblNCcounties.C ountyID WHERE (((tblNCcities. CityName) "
MySQL = MySQL & "= GetMyString())) ORDER BY
tblNCcities.Cit yName;"

then assign a combo box its rowsource or do a RunSQL command, this
global var is a handier than a pocket on a shirt. I don't have to
dimension it or anything - its there when I need it, so I just use it.
Its a habit. Even though I think its value is assured after assigning
it, I do realize that in some cron-based event running somewhere
else, it might possibly be changed. I guess that, in itself, is reason
enough to abandon the practice. But it sure will be a hard habit to
break. Any more comments - anybody?
The problem is that global variables tightly couple together code that does
different things at different times, and since any code can update the
variable at any time, you can never be sure it was left in the state you think
it was. Generally, it's best if you can arrange for each piece of code to
send data directly to the thing that will use it.

One abuse of globabl vairables I've seen frequently is to keep track of the
last active control. The trouble with this is, what happens when you toggle
the focus between forms? The right place for this variable is in the form's
module, where there is no confusion as to what form's state we're talking
about.


Nov 13 '05 #4
MLH wrote:
Any more comments - anybody?


Some have suggested using a hidden form to hold global variables. BTW,
has anyone used a hidden bound form holding global variables as a
persistent connection to aid A2K3 in getting data?

James A. Fortune

Cmabrigde Uinervtisy rscjeearcj"

http://www.mrc-cbu.cam.ac.uk/~mattd/Cmabrigde/

Nov 13 '05 #5
MLH wrote:
I've read some posts indicating that having tons of GV's in
an Access app is a bad idea. Personally, I love GVs and I
use them (possibly abuse them) all the time for everything
imaginable - have been for years. If the machine has memory
to spare and windows can use it - I'm thinking "Why not?"

I was wondering what some of you have to say about that,
particularly any severe "gotchas" you've had the unfortunate
experience to contend with.


I should think that its A-OK as long as no code breaks and you go into
Debug mode. Then your vars get reset.
Nov 13 '05 #6
ji********@comp umarc.com wrote:
has anyone used a hidden bound form holding global variables as a
persistent connection to aid A2K3 in getting data?


That idea didn't make sense. The table for holding the global
variables has to be local rather than linked and that would defeat the
purpose.

James A. Fortune

Nov 13 '05 #7
MLH
Valid point. It has been made in this NG before. But I've gotta go now
and clean up about 5-months worth of piss-poor DLookup statement
designs. !;Da&%*!!!

I should think that its A-OK as long as no code breaks and you go into
Debug mode. Then your vars get reset.


Nov 13 '05 #8
MLH <CR**@NorthStat e.net> wrote in
news:uv******** *************** *********@4ax.c om:
I've read some posts indicating that having tons of GV's in
an Access app is a bad idea. . . .
Yes. It's a terrible, terrible practice.
. . . Personally, I love GVs and I
use them (possibly abuse them) all the time for everything
imaginable - have been for years. If the machine has memory
to spare and windows can use it - I'm thinking "Why not?"
Because they represent poor design of your application.
I was wondering what some of you have to say about that,
particularly any severe "gotchas" you've had the unfortunate
experience to contend with.


1. Global variables should *never* be used for merely passing data
between different forms.

2. Global variables should never be used for data that is volatile
and changes during a data session. Most static global variables
should be global constants, and the only data that should be in
global variables is non-permanent values that are looked up and set
at runtime on the opening of the application.

Now, it is sometimes the case that it's important to have data
stored in memory that is used by more than one form, but for this I
always use class modules as data storage structures. A common case
of this is report filtering. I use a dialog form for collecting the
filter ctieria and store the criteria in a class module. The report
then talks to the class module to figure out what filter it should
use.

Since the reports and forms are all using a single class module as
an intermediary, this means that multiple reports can use the same
class module, and it can have as many individual instances as
necessary (something that's not possible with global variables),
and
it also means the the report doesn't need to know anything about
the
filter dialog form, nor the form anything about the report. Both
need know only about the class module that is used to store the
data.

Indeed, in general, I make the dialog forms completely stupid, and
put all the code that pulls data out of them in the subroutine that
opens the dialog. This makes it possible to use a single dialog
form
in multiple contexts without the form needing to know anything at
all about those contexts.

So, something like this (where the close button of the dialog
doesn't actually close the form, just make it invisible, and the
Cancel button sets the form's .Tag property to "Cancel":

' this line needs to be in a public module
Global clsReportCriter ia As New clReportCriteri a

' this code is wherever you're printing your filtered report
DoCmd.OpenForm "dlgReportCrite ria", , , , , acDialog
If Forms!dlgReport Criteria.Tag <> "Cancel" Then
With Forms!dlgReport Criteria
clsReportCriter ia.Criterion1 = !txtCriterion1
clsReportCriter ia.Criterion2 = !txtCriterion2
End With
DoCmd.OpenRepor t "MyReport"
End If
DoCmd.Close acform, "dlgReportCrite ria"

In the OnOpen event of MyReport, you'd check to see
clsReportCriter ia has any values set and use those for filtering
the
report's recordset.

In the report's OnClose event, you'd have this:

Set clsReportCriter ia = Nothing

Now, you will note that I"ve replaced a collectin of Global
variables with a Global instance of a Class Module, so you might
say
that I'm just doing a more complex version of the same thing.

But the added complexity gives you full control over the scope of
the values you store in the globally available variables.

And that's the chief problem with bare Global variables -- they are
too easy to poke new values into that could break other code that
depends on values having been put in them. With multiple class
instances, you can control exactly which instances of the variables
you want to use, and you can easily set them back to nothing.

For globals that you set at application initiation from some form
of
stored value, it's better, in my opinion, to replace them with
self-healing functions that use Static variables insternally. Say
you have an application name that you use in a lot of report or
form
headers. You could have a Global like this:

Global strAppName As String

and in your initialization routine assign a value to it like this:

strAppName=DLoo kup("KeyValue", "tblSetting s", "[Key]='AppName'")

Now, I"d replace the global variable with a function:

Public Function strAppName () As String
Static strApplicationN ame As String

If Len(strApplicat ionName) = - Then
strApplicationN ame = DLookup("KeyVal ue", _
"tblSetting s", "[Key]='AppName'")
End If
strAppName = strApplicationN ame
End Function

This gives you 3 advantages:

1. you don't need global variables for it, since the wrapper
function keeps the static variable hidden to itself.

2. oif converting from global variables to functions like this, you
don't have to change code that uses the global variable, if you
make
the function name the same as the former globabl variable.

3. you no longer have to initialize the values for these global
variables, as the function is self-healing -- it fhe value hasn't
been set at the first call to the function, it is looked up and set
then. This means the first time your app uses it in a session, as
well as after code resets during the development stage.

So, basically the only Globals I have in any app are globals for
data structures that are used in multiple places. This would mostly
be arrays and class modules.

Of course, I also have a lot of applications in production use that
date from the time when I didn't know any of these techniques, so I
do have to deal with global variables all the time. But it is a
real
headache and I try to engineer them out of the way weenever
practical.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #9
MLH <CR**@NorthStat e.net> wrote in
news:19******** *************** *********@4ax.c om:
What about these 3: GlobalString, MySQL and GlobalVariant. I use
them really for convenience more than anything. Anytime I want to
concat a bunch of crap like

MySQL = "SELECT tblNCcounties.C ounty,
tblNCcities.Cit yName
FROM (tblNCcities LEFT JOIN tblNCcityCounty JunctionTable "
MySQL = MySQL & "ON tblNCcities.Cit yID =
tblNCcityCounty JunctionTable.C ityID) LEFT JOIN tblNCcounties ON "
MySQL = MySQL & "tblNCcityCount yJunctionTable. CountyID =
tblNCcounties.C ountyID WHERE (((tblNCcities. CityName) "
MySQL = MySQL & "= GetMyString())) ORDER BY
tblNCcities.Cit yName;"

then assign a combo box its rowsource or do a RunSQL command, this global var is a handier than a pocket on a shirt. I don't have to
dimension it or anything - its there when I need it, so I just use it. Its a habit. Even though I think its value is assured after
assigning it, I do realize that in some cron-based event running
somewhere else, it might possibly be changed. I guess that, in
itself, is reason enough to abandon the practice. But it sure will be a hard habit to break. Any more comments - anybody?


Been there, done that, and found that even though I thought I was
being careful to assign the value each time I used it, I ended up
with legacy values from other contexts.

This is the absolute *worst* tyhpe of usage of a glabal variable,
because the problems that can result from a breakdown are so hard
to
troubleshoot and will be dependent on the order of events that
happened before you used it.

This is the first kind of global variable that you should
eliminate.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
3331
by: lkrubner | last post by:
If I set a variable at the top of my code like this: $name = "Lawrence"; It is now a global variable. If, later on, in a function, I want to do this: function uppercaseName() { global $name;
0
1357
by: Kepes Krisztian | last post by:
Hi ! How to access my module global vars ? Some applications I want to declare global vars (constants). But in the module I cannot access them: DEF_X=120
6
2026
by: flamesrock | last post by:
ok, so to my knowledge, object oriented means splitting something into the simplest number of parts and going from there. But the question is- when is it enough? For example I have the following code: #def put_file(file_id, delete=False): # """ Function to put the file on the FTP Server # """ # print " FTP for this file started"
10
2641
by: Kleenex | last post by:
Reason: I am working on an embedded project which has very limited memory (under 512 bytes, 60 or so of which is stack space), which translates into limited stack space. In order to save on stack space, I tried to only use parameters and stack space for things which are truely temporary. Instead of passing a pointer to a data structure which should always be populated with data, I have the data structure declared as a global variable and...
6
1964
by: rlrcstr | last post by:
The DBA team at the office controls the naming conventions for the database structure, but their naming convention are rather tedious. So typically I create a global module that I use as a mapping file to create constants with more appropriate field and table names and map them to the table and field names required by the DB team. But now, using a DataList, I'm having a problem. How can I use a variable name to indicate the field name in...
18
2945
by: robert | last post by:
Using global variables in Python often raises chaos. Other languages use a clear prefix for globals. * you forget to declare a global * or you declare a global too much or in conflict * you have a local identical variable name and want to save/load it to/from the global with same name * while you add code, the definition of globals moves more and more apart from their use cases -> weirdness; programmers thinking is fragmented * using...
9
8654
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang Su Gatlin, casual mention was made about using static variables as an alternative to using global variables. This caused me to think of the following: '-----Begin module code
9
1987
by: David | last post by:
With a non-server app there is one instance of the program running and one user 'using' it at a time. With this scenario I'm pretty comfortable with variable scope and lifetime. With a server app there is one instance of the program running but several simultaneous clients connecting to and 'using' it. When I think about this I'm wondering what this may add to what needs to be considered for scope and lifetime... is a scenario created where...
4
1754
by: pcaisse | last post by:
I'm having issues sharing global variables with Explorer. This problem probably has a simple answer (as with most newbie questions). The script.pl file: #!/usr/bin/perl -w use strict; use diagnostics; use sigtrap;
0
8784
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9462
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9326
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9248
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8199
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6049
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4821
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3270
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2738
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.