Automatic Code: formOPEN

It looks like the code in the formOPEN section of a form event procedure does not run if the window is opened by default when the database is first opened. It would be nice if it did,

I could see where this could be problematic in execution. Would that code temporarily interrupt the .Initialize procedure running at startup or be run after the .Initialize procedure? In either case it could cause problems when executing. If interrupting the .Initialize procedure it might find things to be different than expected when the .Initialize is finished running. If it runs at the end it might find that the form window is no longer the active window or the form itself has been changed by the finished .Initialize code. I’m sure there are other technical land mines to further consider when having two automatic procedures trying to run at the same time. :face_with_peeking_eye:

I imagine you could make sure your formOPEN code accommodates these situations but many others would never know these problems could potentially occur and end up with a real mess to figure out. Probably what you want to do with the formOPEN code could be handled in the .Initialize procedure itself by first checking to see if that form has been opened. If the form is opened procedurally or manually later on, the formOPEN code will function as expected.

I’ve turned to using small “Initializing” windows until the stage is set. Then I open my key form and show any variables or other necessary steps to presenting the form as desired. formOPEN could possibly trigger at that point but I suspect you’ll see your current results. You could always end your Initialize with the same code as is in formOPEN.

Or … maybe you could call the same procedure from the initialize and FormOpen and that procedure would do what needs to be done in one place.

That way, if “what needs to be done” changes - and it will - you only have to change it in one place instead of remembering the code is in two locations. :slightly_smiling_face:

This is all a nice discussion, however, it’s based on an original incorrect observation. The formOPEN code does run when the database is first opened. I just checked the source code, saw that it should, and then verified that it does in fact run when the database opens.

Perhaps the problem is that you have more than one form that opens automatically. In that case, it’s not guaranteed what window is active when the code runs – it may be some other form. If you’re using this code to do things like adjusting objects on the form you should put that code in the formFRONT section.

There is only one window in the Initial Form section of Database Options. The code in the FormEvent section defines one WindowGlobal variable:

formOPEN:
   letwindowglobal wgud="u"
rtn

When I try to use wgud, I get an error message that the variable does to exist. If I then close and open the form, wgud does exist. That is why I concluded the code was not running in the first instance.

15 minutes later
Okay, now it is working as expected! Sorry for wasting your time.