Form Events

This tip ‘Form Events’ is suitable for all versions of Microsoft Access

This tip, Form Events, is a suggestion from one of Simply-Access tips subscribers.

The following is a condensed version of part of one of the VBA Lessons it should give you an idea of what events are and what they can be used for.

Definition: Events are things that happen to objects, such as the clicking of a command button or the opening and closing of a form, etc. These events trigger an action to be carried out.

This action is in the form of a Macro or an Event Procedure. In this Newsletter we will be concerning ourselves with Event Procedures only.

You will often see events written with the ‘On’ Keyword, such as ‘On Open’, ‘On Current’, etc.

Form Events

The Event we are going to concentrate on for this tip are Form Events and, in particular, the events that occur when a form is opened.. A ‘Form Event’ is an Event that can occur within a form.

Events occur for forms when you open or close a form, move between forms, or work with data on a form. An easy way to view the available Events for a Form (Control or Report) is to open the properties window of the Form in question.

To do this right click in the upper left hand corner of the form, where the two rulers meet, and select ‘Properties’ (left click) from the list displayed;

With the Properties Window open, select the Event Tab. You will see the list of event displayed

With Events, it is important to remember that they occur in order and the attachment of your code to a particular event will set when that code is run. Sometimes it can be a bit of trial and error to determine where the best position is to place your Macro or Code.

If you open a Form, the following Events occur in the following order:

  • Open
  • Load
  • Resize
  • Activate
  • (GotFocus)
  • Current

If there is no active Control on a Form, the GotFocus event will occur. If there is an Active Control, the GotFocus Event will not occur. This is because the Focus is on the form rather than a Control, and therefore this Event can occur if the focus is not on an on object within the form.

Open Form Events

On Open Event

The Open Event occurs when a form is opened, but before the first record is displayed. Therefore, attach code here that you wish to run as soon as the form is opened. The Open event will not occur when you activate (move to a previously opened form), i.e. if you open a second form from the first form, then close the second form, the first form’s On Open Event will not occur as the first form has not been closed and re-opened, it has just been hidden behind the second form.

If the Form is based on a Query, the Query is run prior to the On Open Event.

On Load Event

Whereas the on Open Event occurs when the form is opened and before the first record is displayed, the On Load Event occurs when the first record is displayed.

On Resize Event

The Resize Event occurs when a form is opened or whenever the form’s size changes.

On Activate Event

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

You make a form active by: Opening it; Clicking on form with your mouse, or clicking a control on the form; Invoking the SetFocus method. The On Activate event can only occur if the form is visible. If the form is not visible an error will occur.

On GotFocus Event this rarely used Event only occurs when the form gets focus, but only if there are no visible enabled controls on the form. Which is unlikely.

On Current Event

The On Current event occurs when the focus moves to a new or different record making it the current record, or when the Form is Refreshed or Requeried.

This Event occurs when a form is opened, whenever the focus leaves one record and moves to another, and when the Form’s underlying Table or Query is requeried. This event is one of the more commonly used Events. If you wish to run code whenever a record is displayed, this is the place to put it.

Return from 'Form Events' to Simply Access Home Page


Return to Microsoft Access Forms


Return to VBA Code