Global variables for database

mcdhappy80

Registered User.
Local time
Today, 01:18
Joined
Jun 22, 2009
Messages
347
I was wondering can this be done:
I want to create a Standard Module which I will call GlobalVariables and then create some variables in it and store values in those variables from other forms and reports in mt database.

What I don't know is how (what is the code syntax) and from where (form, report) to assign the values in those variables that are in GlobalVariables module. Also is there a specific code syntax if I want to refer (read or use) to those variables from within a form or report?

If question was not understandable please say so and I will try to explain better.

Thanks.
 
Once you declare the variable as Public in the beginning of a module it becomes available to the entire project.

You simply write the variable values into a text box in the report or form.
Me!Mytextbox = MyGlobalVariable
 
You also should come up with some naming syntax convention to make identifying your variables easier. One common scheme is to prefix module level variables with 'm' and global variables with 'g'. This would be the three different versions:
Dim sMyString as String 'Procedure level declaration, only visible in procedure
Private m_sMyString as String 'Private to module, visible to all procedures in module
Public g_sMyString as String 'Global to all modules (even visible outside app if using automation).
This will prevent confusion if using similar or same variable names (say, sCustomer may be used many different ways).
 
in a code module declare the variable as public

thus

dim mynum1 as long is private but
public mynum2 as long is visible by anything in your database

however to use a variable as a criteria in a query you need a function

so

function readmynum2() as long
readmynum2 = nz(mynum2,0)
end function


and then in your query use
=readmynum2()
 
Dave,
Do you actually read the posts or the answers? Your answers either repeat something already said or answer a situation not described in the question.
 
Dave,
Do you actually read the posts or the answers? Your answers either repeat something already said or answer a situation not described in the question.

lol

he did ask

Also is there a specific code syntax if I want to refer (read or use) to those variables from within a form or report?

i thought it worth pointing out that you cant just use the variable directly in a query - you have to dereference it with a function - although this was slightly off topic, its not obvious.
 
A further note of some issues that I tripped on recently regarding this same topic.

Having global variables declared properly in a general module doesn't initialize their values because there is no "automatic" code in a global module. I.e. no module runs just because the DB got opened, with one exception - a startup form. You need something active to do your initialization, like the Form_OnLoad or _OnOpen event of a startup form or the same routines on some form that you call manually to INIT everything.

If you are debugging, though, and happen to perform an action that triggers the message box "This action will reset your project" message, you just lost all your global values. I believe if you have an unhandled error trap, that also does it to you big time.

Also, if there is ever a time when you DON'T have any code modules open, I believe you can lose the global stuff, because the moment the last code module closes, the values go away. This would happen, for example, if your DB has NO forms and NO reports open. I.e. you just closed the last form or report so that your DB window shows zip, zilch, nada open items anywhere.

Since I don't play with class modules, I can't speak quite as authoritatively regarding them, but unless you have instances of the class defined/instantiated, it doesn't matter. No code is running there, either.

I resolve this by having a startup form that minimizes itself (NOT closes itself) and never goes away until you close the DB. The startup form's timer routine issues notices where appropriate for my ap. That can happen because as long as the form is open, code can run. You can also do this from a macro, but they tend to be clunky mothers that have limited or no error handling. A small startup form is a better choice for simplicity.
 
its a good point docman

i have taken to checking some important values when i open particluar forms. and reinitiallising them if they are not set.

This is partly to make like easier while developing, in cases when the variables have fallen out of context for the reasons you describe, and practically to ensure the forms continue to work properly in all circumstances.
 

Users who are viewing this thread

Back
Top Bottom