Why are form event codes so confusing?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

As a newbie, I struggle where to put code when a form is initialised (I have
used this term as a generic one as, to my knowledge, there is no <On
Initialise> event).

There are so many events which *seem* to be the same, even though I know
they're not. For instance, On Load, On Open, On Activate, On Current, On Got
Focus all seem as if they should be the same. Does anyone else have the same
struggle? Many events are self-explanatory but, apart from placing code in
"initialising" events by trial and error to see what works, is there any
logical way of KNOWING where code should go to do specific actions?

<Personal rant over!>

Thank you.
 
The Help file contains relatively good explanations of the differences
between the events (although I'll admit that in certain versions of Access,
it's hard to find things in the Help file!)

The Open event occurs when a form is opened, but before the first record is
displayed.

The Load event occurs when a form is opened and its records are displayed.

The Resize event occurs when a form is opened and whenever the size of a
form changes.

The Activate event occurs when a form receives the focus and becomes the
active window.

The Current event occurs when the focus moves to a record, making it the
current record, or when the form is refreshed or requeried.

When you first open a form, the following events occur in this order:

Open >> Load >> Resize >> Activate >> Current

Note that the Open event doesn't occur when you activate a form that's
already open - for example, when you switch to the form from another window
in Microsoft Access or use the OpenForm action in a macro to bring the open
form to the top. (Nor do the Load or Resize events). However, the Activate
event does occur in these situations.

I will agree that the GotFocus event (which occurs when a form or control
receives the focus) is pretty much unnecessary, as it's no different than
the Activate event.

Actions that need to be done at intialization, therefore, should probably be
place in the Open event.
 
Thank you Doug. I realised that there's an order in which the various
actions occur. With your explanation, maybe I'll be able to determine
exactly where to put the code that I create or tweak from samples that I see
around. I agree that the Help file isn't very user-friendly and perhaps
that's why there are so many apparantly straightforward questions asked here
- including mine!
 
Douglas J. Steele said:
Actions that need to be done at intialization, therefore, should probably
be place in the Open event.


With one caveat -- controls and data usually are not available in the Open
event, so any code that is writing values to controls or fields, or reading
values from controls or fields, should be done in the Load event.
 
Ken Snell (MVP) said:
With one caveat -- controls and data usually are not available in the Open
event, so any code that is writing values to controls or fields, or
reading values from controls or fields, should be done in the Load event.

Good point, Ken.
 
With one caveat -- controls and data usually are not available in the Open
event, so any code that is writing values to controls or fields, or
reading values from controls or fields, should be done in the Load event.

yes...great point. What this really means is that the open event can be used
to "test" conditions, and check values of fields. If those values are not
meet, then you can set cancel = true, and the form WILL NOT load.

This really means two things

Forms setup and initialing code really belongs in the on-load event.
Save the on-open for form cancel and testing of certain conditions. This
also cleans up code that belongs in the on-load.

Since the form "open" can be canceled, then it makes sense that you
can not modify values (what would happen with a cancel = true in this case).

So

on-open - check values...possibility prevent form from loading

on-load - forms setup code...can modify values of fields and
controls. Set values of variables...basic setup stuff goes here


If the help file explained it as above....it would be quite clear......

Further, it important to note that the form is NOT considered loaded, and
active until the on-load event is done. Which means screen.ActiveForm is
STILL not yet set in the on-load event....
 
Back
Top