junos

implement IT today

Microsoft Access Form Events Event Handling Macros in Access Visual Basic

When you’re designing a form in Microsoft Access, you can put onto it things like labels, textboxes, buttons and comboboxes. The generic name for all fo these things is called a control.

Both the form itself – and each individual control on it – has a whole set of events which can happen in its life. These are similar to the events which can happen in your life. For example, for a human the main events are:

A typical event for an Access form is the OnOpen event, which runs whenever you open the form. Typical code that you might attach to this might be to hide or display certain controls dependent on who the user is or what they’re doing.

Certain events are divided into two parts: Before and After. For example, a form has got a BeforeUpdate and AfterUpdate event. Imagine that you fill in the details for a person in a contact form in Access, then click on the button to move onto the next blank record. This will automatically trigger the BeforeUpdate event. At this point validation code could determine that you haven’t filled in the form correctly (perhaps you’ve missed out the person’s surname, or put in a date of birth in the future), and cancel the update.

Alternatively, the BeforeUpdate event could run unimpeded, and at this point Access will save the record and then move on to the AfterUpdate event. You can attach code to this event to react to what has just occurred, but it is to late to stop the update happening.

This is what happens for form events, but the same is true of control events. For example, if you have a text box where you have to type in someone’s name, you might inadvertently type in too many characters, or a number. When you press TAB to move on to the next control, the control’s BeforeUpdate event will kick in, at which point you can check that the name typed in is acceptable (and if not, cancel the event). If, however, the control passes this test, then Access moves on to the AfterUpdate event. Typical VBA code would then change the value of other controls accordingly.

Other events include:

Once you’ve mastered what’s happening in MS Access forms, you’ll be ready to become a serious Access developer!


Leave a Reply