Form_Open vs. Form_Load

Posted on 2006-05-04
Last Modified: 2012-06-21
I got handed a half completed Access application and I have little experience with Access and VB so I'm learning as I go.  What is the difference between form_open and form_load?  I'm guessing that I should put different functionality in each? Something like setting up the layout of the form with form_open and then doing any field manipulation or database coding in the form_load??

Thanks in advance for any information provided.  
Question by:tim_cs
    LVL 5

    Assisted Solution


    The Form_Load is mainly used for the coding purpose, This event will be called when the form is loading, what ever the code you put in this will be exectued before the form is open

    The Form_Open is also a event that will be trigger when the form is completed the loading and above to open. We also can write code in this, but more is the form layout and settings.
    LVL 77

    Expert Comment

    Hi tim_cs,
    But you create a form using the form designer.
    There is not necessarily any code involved at all.

    LVL 14

    Assisted Solution

    from access help:

    When you first open a form, the following events occur in this order:
    Open → Load → Resize → Activate → Current

    When you close a form, the following events occur in this order:
    Unload → Deactivate → Close

    the difference between Form_Open and Form_Load is that Form_Open has a Cancel parameter which you can set to True in order to cancel the opening action.  e.g., say, if a user wants to open a form to see entries in a table, you can use Form_Open to check if there are any entries in that table at all; if there are *no* entries, you can display a message and then set Cancel = True so that the form will not open.

    LVL 14

    Expert Comment

    oops, apparently, there were special chars in my last post: the → were originally arrows, so the whole thing should read:

    When you first open a form, the following events occur in this order:
    Open -> Load -> Resize -> Activate -> Current

    When you close a form, the following events occur in this order:
    Unload -> Deactivate -> Close

    LVL 16

    Accepted Solution

    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...
    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...
    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.

    LVL 74

    Assisted Solution

    by:Jeffrey Coachman
    <The Load event fires after the form is actually Open and after the records have been displayed>
    Well... not really, as you probably know. All of these events actually fire before the form is actually visible on the screen.
    (I put a message box on each event)

    So when they say "after the records have been displayed", they must not consider "displayed" the same as "visible".
    Perhaps when they say Displayed or Open, they mean Displayed or Oppen in Memory?
    I'm still waiting for the Form "On Appear" event. So I can pop up a message box right after the form is visible!

    Another curious thing is the timing.
    Take the onClose Event:
    <The Close event occurs when a form or report is closed and removed from the screen>
    The OnClose event actually fires "Right Before" the from is removed from the screen.
    (When the event fires, the form is still visible.)

    Now, none of this really matters to seasoned developers, but to a new developer it might be confusing.

    Again, when I first read about the On Open Event (after the records have been displayed) I wanted to display a message saying "Here are all the records!"
    But boy was I suprised and embarrased when the message box showed up with no form.

    I thought that putting the message box on the OnOpen event would open the Form and the messagebox at the same time.

    (Yes, I know the message box is modal, but the help file led me to belive that they would at least show up at the same time.)
    Ok, I'm done with my rant...


    The great thing about all of these events is that you can simply click on the Event (or any property for that matter), press the F1 Function key and get help on it.

    Good luck!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Suggested Solutions

    When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
    Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
    Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now