How/where to initialize a global variables

ZikO

Registered User.
Local time
Yesterday, 20:53
Joined
Dec 15, 2012
Messages
41
Hello,

I have the main form 'frmMain' with a subform, e.g. frmSubform and a few global variables such as 'bAreTextBoxesLocked' (boolean) and 'collcolorThemes' (Collection) in a module. I created a public procedure in this module where I initialize them. Then I need the main form and the sub form to read the variables and set a few controls depending on value of 'bAreTextBoxesLocked', whether it is True or False.

So far I thought the best would be OnOpen event but somehow one of the forms tries to access to the variables before they are set, especially Collection is not set.

Is a solution to this to create one more form 'frmInit' that opens another form after all variables have been set? Or maybe there is a easier/better solution?

Thanks in advance for any help.
 
Public variables are not going to be Public unless they are being initialized/declared in a Module. If they are declared in a Form. They are quiet pointless, its scope is limited to the Form only. So this is what you need to do.

1. Create a function in a (new or) existing module. Something like,
Code:
Public Function initVars()
    myPubVar1 = 10
    myPubVar2 = 20
End Function
2. Create an AutoExec Macro to run the function.

You are done !
 
Hi pr2-eugin,

Thanks for your answer.

I test when AutoExec runs when I open the database and it seems to be when everything is loaded, subform, main form and at last Nav form. I checked that putting comments in OnLoad and OnOpen event codes. It does not work for me. I need to initialize variables before the form is loaded.

Before I try to open hidden form, is there any easier solution?

Thanks
 
I test when AutoExec runs when I open the database and it seems to be when everything is loaded, subform, main form and at last Nav form. I checked that putting comments in OnLoad and OnOpen event codes. It does not work for me. I need to initialize variables before the form is loaded.
Say What? :eek:

AutoExec macro is the first Macro that runs when the DB starts, yes that also include before the Forms are loaded. Did you try the method? What do you mean it does not work for you?
 
Say What? :eek:

AutoExec macro is the first Macro that runs when the DB starts, yes that also include before the Forms are loaded. Did you try the method? What do you mean it does not work for you?

The order the code runs is following:
Sub Form, OnOpen
Sub Form, OnLoad
Main Form, OnOpen
Main Form, OnLoad
Navigation Form, OnOpen
Navigation Form, OnLoad
Initialize Globals (executed in AutoExec)


I did try "frmInitialize" that is a simple form without anything but OnLoad code and AutoExec was first macro to run. When I use the Navigation Form with main form and sub form, somehow Auto Exec runs after everything is Opened and Loaded, as the last macro!!!

Can I open form when database starts and hide it. I could initialize everything there, open the navigation form and close the frmInit. Can I do it?
 
Okay let us tackle it the way you have, Forget about the AutoExec macro. In the Form Open method of the frmInitialize, call the function to initialize the variables.

Then in the Form Load of the frmInitialize use DoCmd.OpenForm to open the Navigation Form. See if this works as intended.
 
What I usually do in a situation where I need globals loaded ASAP is create a Startup function that is called/executed via a one-line AutoExec. The startup ensures that the globals are initialized when it kicks off. Then I do whatever else I need to do at startup (usually version and link checking), then I have the end of the startup function open the main menu directly.

This does mean that I don't have the database set to open a form at start-up, by the way.

And maybe it's just me, but I tend to put all my globals in a single module (modGlobals or basGlobals, as appropriate) that contains nothing BUT Public declarations.
 
Thanks guys. I trierd the frmInitialize and it worked but Frothingslosh's solution is also very good. I made a global function where I initialize global variables and then open the navigation form. It's working now. THanks for help :)
 

Users who are viewing this thread

Back
Top Bottom