473,325 Members | 2,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Globals and system instability

DFS
Not sure whether it's bad programming practice or not, but I have a module
of globals I declare in each system:

Global ws As Workspace
Global db As Database
Global td As TableDef
Global rs As Recordset, rs1 As Recordset
Global rs2 As Recordset, rs3 As Recordset
Global ctl As Control
Global frm As Form, subFrm As Form
Global rpt As Report
Global qItem As QueryDef
Global menuObject As Object

Most of them get used in each Access app, so I don't feel bad about
instantiating them this way. Saves some development time and hassles.

In general:
* I Set the db object to CurrentDB when opening forms, and never Close it.
* I always Set and Close the recordset objects I use.
* I Set the frm references as necessary, but never reset them to Nothing.

I'm wondering if declaring and using Global objects this way contributes to
system instability? When my apps reach a certain arbitrarily large size
(lots of forms and code), I always, always get the dreaded corruption
screen.

Usually it's recoverable, but sometimes not. And it always causes me to
curse.

Thoughts?

Nov 13 '05 #1
2 1637
I am not aware of the approach causing instability in terms of ms-access.

However, use of global as a general rule is frowned upon. It makes the code
less modular, generally harder to debug, and harder to maintain.

Further, it also tends to make the programming process harder, as you have
to ensure that no other code is using those variables. If other code needs
those variables at the same time, then you are toast. So, it may not make
your application less stable, but it is sure fire way to introduce hard to
find bugs, and increase general maintaining costs, and simply increases the
difficulty in moving code from on application to another.

Even worse, is what about importing and using other code? If you get name
conflicts, then again confusion arises.

Global variables should ONLY be used for things that need to be global. So,
for example, you do not want to use global to pass variables between forms
(besides, you can set a reference to the other form, and use any variables,
or procedures in that form anyway - so, you don't even have to pass
vars...just references the previous form).

So, yes, as general approach to software and programming, global are much
frowned upon. And, the approach makes your code MUCH MUCH less modular. If
you take only ONE of those routines, then all the global have to follow
(and, you got to figure out what global must be moved, and you got to go
find those global).

Further, how can you have two routines operating at the same time? If you
got global, then you can only have ONE routine that users the "ctl"
(control) variable at a given time. Really, next to impossible to maintain
such systems.

When a developer working for me brings code with UNNECESSARY globals, I send
the code back with instructions to remove them....
--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
pl*****************@msn.com
http://www.members.shaw.ca/AlbertKallal
Nov 13 '05 #2
DFS
Albert D. Kallal wrote:
I am not aware of the approach causing instability in terms of
ms-access.
OK. Something is causing me mucho corruption, and it seems haphazard, but
I'm almost sure it's related to a module, as the .mdb will corrupt when I
try to compile a module, or load a form that uses a module.

It's very frustrating.
However, use of global as a general rule is frowned upon. It makes
the code less modular, generally harder to debug, and harder to
maintain.

Further, it also tends to make the programming process harder, as you
have to ensure that no other code is using those variables. If other
code needs those variables at the same time, then you are toast.
Nah. Just create another global, and increment it with a number...:)

So,
it may not make your application less stable, but it is sure fire way
to introduce hard to find bugs, and increase general maintaining
costs, and simply increases the difficulty in moving code from on
application to another.

Even worse, is what about importing and using other code? If you get
name conflicts, then again confusion arises.

Global variables should ONLY be used for things that need to be
global. So, for example, you do not want to use global to pass
variables between forms (besides, you can set a reference to the
other form, and use any variables, or procedures in that form anyway
- so, you don't even have to pass vars...just references the previous
form).
I use two "types" of globals: 1) Access objects as I listed in my original
post, and 2) a few string variables like cSQL (for character SQL statement).

I do mostly use form references to get things like ID values from tables.
But to reference the form itself, I often set and use a global called 'frm'.
But I never Set frm = Nothing. That was the point of my post: does
declaring and setting but not closing a variety of Globals introduce
instability? I would think not, but something's hammering my systems.
So, yes, as general approach to software and programming, global are
much frowned upon. And, the approach makes your code MUCH MUCH less
modular. If you take only ONE of those routines, then all the global
have to follow (and, you got to figure out what global must be moved,
and you got to go find those global).
Sure. So I start each new Access system I create with a standard set of
modules :

- General Declarations (includes the Globals)
- routines to manipulate Access objects
- routines related to file dialogs and browser
- things related to startup (disable built-in toolbars, etc)

A bit of it goes unused, but it's not much code.
Further, how can you have two routines operating at the same time? If
you got global, then you can only have ONE routine that users the
"ctl" (control) variable at a given time.
It's rare - next to never - that I need two routines to be running together.
If one is nested inside another, obviously I use Globals ctl1 and ctl2, etc.
Really, next to impossible
to maintain such systems.
It hasn't been a problem for me over the last 5-8 years. But in the past
year or so, I'm getting frequent corruptions whenever my Access projects get
large. I haven't been able to resolve it, and it takes too much time to
recreate the same system without the use of the globals. If that's what it
is.
When a developer working for me brings code with UNNECESSARY globals,
I send the code back with instructions to remove them....


Unnecessary is the key word. I like the judicious use of globals, but I may
try to reduce them as a test.

Thanks for the reply, Albert.

Nov 13 '05 #3

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

Similar topics

5
by: Frostillicus | last post by:
I'm trying to use array_multisort to sort by one of the dimensions of an array stored in $GLOBALS like this: array_multisort($GLOBALS, SORT_STRING, SORT_DESC); Each "row" in $GLOBALS contains...
7
by: John | last post by:
Hi, I'm looking for the best way to deal with globals in PHP. As a 'C' software developer, I would normally avoid all globals and not have any at all, but use structs and pass everything in...
45
by: It's me | last post by:
I am new to the Python language. How do I do something like this: I know that a = 3 y = "a" print eval(y)
11
by: Matt | last post by:
Hello, I'm rather new to C++ and have a question about globals. What is the big deal about them? They make things much easier to program because you don't have to worry about all the passing...
1
by: gianguz | last post by:
In the following example Global is able to create and manage access to objects of any kind (even in a multithreading environment) with and index value attached to. So a Global<0, string> is a...
8
by: Michael Wild | last post by:
Hi all I'm not quite new to PHP, but also not very proficient in its use, so please excuse me if my question is a FAQ. Perhaps I didn't know the right terms, but google wasn't my friend this...
2
by: xml0x1a | last post by:
How do I use exec? Python 2.4.3 ---- from math import * G = 1 def d(): L = 1 exec "def f(x): return L + log(G) " in globals(), locals() f(1)
5
by: Steven W. Orr | last post by:
I have two seperate modules doing factory stuff which each have the similar function2: In the ds101 module, def DS101CLASS(mname,data): cname = mname+'DS101' msg_class = globals() msg =...
1
by: cokofreedom | last post by:
if __name__ == '__main__': print "Globals (For Loop):" try: for i in globals(): print "\t%s" % i except RuntimeError: print "Only some globals() printed\n" else: print "All globals()...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.