Solved

Using OnOpen vs. OnLoad

Posted on 2010-09-01
3
786 Views
Last Modified: 2012-05-10
In my Access 2007 database, I have several forms and each form has several subforms. I have a lot of VBA code in the OnLoad event of most of the forms/subforms to initialize controls, turn controls visible/not visible under certain conditions, etc.. I also consistently close a form before opening a new one.  I have 2 questions:

1) What is the difference between using OnOpen and OnLoad events in the forms/subforms? Can either be used to initialize controls and set controls visible/not visible or is OnLoad preferred?
2) Is it preferred to close a form before opening a new one if the user is going back and forth between 8-10 different forms?
0
Comment
Question by:newbie46
3 Comments
 
LVL 18

Accepted Solution

by:
Jerry Miller earned 250 total points
ID: 33583373
great explanation of the 2 here:
http://www.experts-exchange.com/Microsoft/Development/MS_Access/Q_22552691.html

If a form will be reused multiple times, I normally hide / unhide them instead of opening and closing.
0
 
LVL 10

Assisted Solution

by:t_hungate
t_hungate earned 250 total points
ID: 33583428
The main difference is when the event are triggered.  For instance you can exit a procedure at OnOpen, because the form is not yet "Open", however you can not cancel an OnLoad, as the form will have been opened and the user will see it, and the error.  

These sites, break it down a little different, but I think that they are good sources.

http://office.microsoft.com/en-ca/access-help/order-of-events-for-database-objects-HP005186761.aspx?redir=0
http://www.accessmonster.com/Uwe/Forum.aspx/access-forms/42103/On-load-vs-On-open
http://database.itags.org/ms-access-database/58505/

Bottom line, is look at the order of events and figure out where the most appropriate place is to trigger your code execution.

Hope this helps.


TLH
0
 
LVL 57
ID: 33585314
Only other thing I would add that I didn't see in those links is that in the OnOpen event, the form is still being initialized, so controls may or may not exist at that point.  You can't count on a control being there until the OnLoad event.
If you need access to a control in the OnOpen event (because you might wish to cancel), you can do a Me.Repaint to force all the controls to be created.
One other important gotcha with subforms; their events occur before the main forms. So when you've reached the main forms OnLoad event, all the other events have already occured for the subforms (Open, Load, Current).
JimD.

0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Familiarize people with the process of utilizing SQL Server functions 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 Microsoft Ac…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

777 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