Question : Form_Current or Form_Open

Hi,
I have date fields in a form, but I want to lock certain date fields after they are filled, if date field is empty then lock is false, do I put code on the Form_open or Form_Current?  what's the difference between Form_open and Form_Current?

thanks

Answer : Form_Current or Form_Open

The Open Event Fires first and has the distinction of being an event that you can CANCEL.  It is during this event that the form is Opening but not yet open, so if you cancel the event by setting the Cancel variable to True.  Just remember that if you do this you will cause the form not to open and cause the calling code (ie: DoCmd.OpenForm "frmMyForm") to fail firing off a trappable error.  If you fail to capture and account for that error your user will be greated with an ugly debug error instead or in the case of a runtime version the app will ungracefully vanish into thin air (closing without any explanation at all).

The Load event fires after the form is actually Open and after the records have been displayed.  Note that the Load Event Can Not be Canceled where the Open Event Can be, (if you didn't cancel in open it's to late to do it during Load).

The Open event behaves a little differently in a Form vs. a Report.  In the case of a Form, the underlying query (data) is executed and loaded as form opens.  This means data from the underlying query is available to you from within the Open Event of the Form.  In the case of a Report however, the underlying query is not loaded when the Open Event fires.  Thus, in the case of a report, the data is not available just yet, however, this also means you could specify criteria for the report as it opens.  Thus in the case of a report you must wait for the Load event to get to the Data, in the case of a form, data is available within both events.

Note that during the Open Event the Form has not yet been displayed.  If you're trying to control how something on the form looks the Open event avails an excellent place to put your code since the user hasn't seen anything yet.  If you wait till Load, the user may see your code in action creating an undesirable visual effect.

Another neat trick has to do with the order of events.  With Forms, events usually fire in the following order...

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

If however, you execute...
Me.Requery
during the Open event, you will cause the Load  >>Resize  >> Activate >> Current events to all fire before the next line of code in your open event runs.

If you execute...
Me.Requery
during the Load event, you will cause the Resize  >> Activate >> Current events to all fire before the next line of code in your Load event runs.

In the interests of providing a more illustrative (if not exotic) example consider this.  Perhaps you want to execute some element of code in the Open and/or Load event to say position the form relative to the MIDI space available (this accounts for toolbars) yet you will discover this doesn't really work because toolbars often change after these events fire and the fact that these events are the logical place for code that would position your form you may find yourself in a bit of a quandary.  In this case you can often Use the Load event to set the Form Timer value to something like 10 or 100 and then sneak your form positioning code in the timer event including code in that event to promptly turn the form timer value to 0 to keep the code from running repeatedly.  In this case the Load event is usually the preferable place to put your code since most everything else is out of the way by then and the code within the load event would look something like…
     Me.TimerInterval = 100

In such a scenario, you might use the Open Event to Hide the form...
   Me.Visible = False

Then use the Load event to....
   Me.TimerInterval = 100

Then use the Timer Event to...
   Me.TimerInterval = 0
   'Some fancy code here to position your form while considering the midi space now that access is done fiddling with the toolbars...
    Me.Visible = True   'This is very important less you'd never see the form open.

***************************

So there you have it, although I have to admit one could say this is merely a brief course of “getting events to dance together 101” but I hope to have illustrated some of the differences between the two events and reasons why your code would be found in the Open Event in some cases and the Load Event in others.  

All this said, and just in case all this makes you a bit nervous, you may be pleased to know that in most cases, most code that you might elect to put in the Open or Load event will work just fine in either event.  It's scenarios like the ones above that help point out that this is not always the case and there are in fact good reasons for each event to exist.

Random Solutions  
 
programming4us programming4us