global variable

icemonster

Registered User.
Local time
Today, 11:56
Joined
Jan 30, 2010
Messages
502
so am thinking of setting a global variable for records opened so that variable will be used on different where clauses throughout the sessions.

any thing i should consider before doing this?
 
What version are you using ? Good idea to put this info in your Posts as the answer often depends on same.

2007 and 2010 have TempVars which earlier versions do not have.

If the Global Variable is for all users to utilise, then consider a Table that can store the data - one or two records.
You could have code to update the field value and date. Code would only do this if the dae was one or more days old.
Table also allows the data to be retained ie only use today's dated variable, if such a record was of use.
 
the reason for the global vars is that i want to use it in where clauses form in vba sql statements so that a certain record (form, unbound populated by the sql statement) can be opened anywhere. although what pitfalls are there to global vars? is there anything i need to consider?
 
I am not sure of all the pitfalls to global vars but I understand you do have to be carful when using same.

As far as having a reference to utilise from anywhere in the database you can do this with a Table.
The benefit of a table is it can hold more information, if this was required and provide an audit trail, if req'd.

All you need to do in your vba code is to declare a variable and then populate it with the data in your table, or use DLookup() to get the value on the table.
 
At any point you use the value of a global variable you have to be sure it has been given a value and in event driven programming where code can execute in various different orders then that can't always be possible. That's why in general they are usually bad practice (it's better to assign values to properties of objects and pass values as parameters to functions).

Anyway, in Access, normally you'd happily ensure a global variable has a value by assigning it one in the start-up form (a global UserID is an obvious example) and then be sure that all code in all other forms would be able to read that value.

The problem is only if the code bugs out. Ending the processing of code loses the values in all variables.

I believe that's the advantage to tempvars (I don't use them much to be sure), that they will retain their values as long as the database (FE) is open irrespective of bug outs.

However, in a good database the code should never bug out :rolleyes: so that shouldn't be an issue and global variables are a little bit quicker and easier and more efficient than tempvars.
 

Users who are viewing this thread

Back
Top Bottom