Creating a global constant

  • Thread starter Thread starter Jeff Harbin
  • Start date Start date
J

Jeff Harbin

Is there a way to create a global variable that would be accessible from
any report or module in a database?

What I want to do is create 2 variables that would define a time frame
that I would use to calculate User defined functions on a bunch of data
that would be bracketed by the two variable which would be Dates/Times
and would change depending on what the user entered.

I've tried filtering the data and putting the calculations in the footer
section for the grouped data but it's still calculating the functions on
the entirety of the data and not the subset which is defined in the
OpenReport function where clause.

The User defined functions use a lot of D functions (DStDev, DAvg, DSum,
etc) and I need to include the date ranges into these functions to get
the right subset.

Thanks

Jeff
 
Is there a way to create a global variable that would be accessible from
any report or module in a database?

What I want to do is create 2 variables that would define a time frame
that I would use to calculate User defined functions on a bunch of data
that would be bracketed by the two variable which would be Dates/Times
and would change depending on what the user entered.

I've tried filtering the data and putting the calculations in the footer
section for the grouped data but it's still calculating the functions on
the entirety of the data and not the subset which is defined in the
OpenReport function where clause.

The User defined functions use a lot of D functions (DStDev, DAvg, DSum,
etc) and I need to include the date ranges into these functions to get
the right subset.

Thanks

Jeff

You can create a module with your public/global variables and any
object in the dB can reference them....
 
You can create a module with your public/global variables and any
object in the dB can reference them....

However if you hit stop while debugging code you lose the assigned
variables. If constants you'll be fine.

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/
 
Jeff Harbin said:
Is there a way to create a global variable that would be accessible from
any report or module in a database?

I've always used a hidden global options form which is bound to my
global options table. And, as required, bound to a query containing
other tables.

I also don't like Global variables as a general rule, except for
specific circumstances, because they require a programmer to change
them. I vastly much prefer them to be on a form so the user can
change if required. Obvious examples would be company name and tax
percentages.

Non obvious examples would be report banding colour (used to store the
amount of grey on each second line so the user can lower the amount of
grey thus updating that number until reports fax just fine and yet the
grey still appears on standard reports), default logo, Idle detect
time out in minutes, default data path and so on.

(Actually tax percentages should be in thier own tables with start and
end dates. Thus when the tax % changes all transactions on that date
or newer will have the new, higher tax amount while delayed entries
backdated a few days will have the old % amount. And have you ever
heard of a tax going down?)

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/
 
Is there a way to create a global variable that would be
accessible from any report or module in a database?

There are two kinds of globals that are OK to use in an app and one
kind that's a bad idea. The good kinds:

1. global constants: these you put in a module and they will always
just work.

2. global properties set at application startup that are needed in
various places in the app.

The kind that is bad is:

3. global variables that are used for passing data between parts of
your app.

Since you're not going to use this last one, I won't explain how to
protect it from code resets.

Constants don't need to be protected from code resets, as they are
always available.

It's only type 2 that needs protection.

The best way to implement them, I believe, is as a class module. The
class's INITIALIZE event will look up the values from wherever you
store them (from a table, as custom database properties, from the
registry, etc.) and assign the values to variables internal to the
class. The class will have properties that return those values. If
you define your class instance's variable with the NEW keyword, any
call to a property of the class after a code reset will
re-initialize it.

Another way to do it is to use functions with STATIC variables. Your
function will check to see if a value has been assigned to its
internal STATIC variable, and if not, look up the value. But each
subsequent call won't need to look it up, but will use the STATIC
variable's value. A code reset will clear the STATIC variable and
the next call to the function will look it up again.

I think it's good practice to put all your application-wide
properties inside a single data structure, and that's why I use a
class module.

But the function/STATIC variable approach is probably a lot easier
for most people.
 
Back
Top