Common Form Events

To cut to the chase, here's a list of commonly used events and how you might want to use them. If you know how to use this basic set of events, you're most of the way there to understanding event-driven programming in Access VBA.

□ Form—On Open: The Open event for a form fires before the Load event, and before the recordset is evaluated for the form. This means you can use this event to change the recordset (by changing the Where or Order By clause) before the form continues to load. This event can be cancelled, which means the form will not continue to the Load event.

□ Form—On Load: The Load event for a form fires after the recordset for the form has been evaluated, but before the form is displayed to the user. This offers you an opportunity to make calculations, set defaults, and change visual attributes based on the data from the recordset.

□ Form—Before Update: If you want to perform some data edits before the user's changes are updated in the database, this is the event to use. All the field values are available to you, so you can do multifield edits (such as HireDate must be greater than BirthDate). If something doesn't pass your validity checks, you can display a message box and cancel this event. This event also fires before a new record is inserted, so you can place edits for both new and changed records here.

□ Form—On Double Click: This is a strange one. If you build a continuous form to display records in a read-only index format, your users will expect to drill down to the detail of the record by double-clicking anywhere on the row. But what if they double-click the record selector (the gray arrow at the left side of each row)? The event that fires is the Form's double-click event. By using this event, you can run the code that opens your detail form. This gives your user a consistent experience and the confidence that your applications work no matter what they do.

□ Form—On Unload: This event can be used to check data validity before your form closes. It can be cancelled, which will redisplay your form without closing it.

□ Form—On Current: This is one of the most overused events by novice programmers, but it does have some good uses. It fires every time your form's "current" record changes, that's the one that the record selector (the gray arrow on the left side of each record) points to. It also fires when your form first loads and positions to the first record in your recordset. One good place to use On Current is on a continuous form where one of the buttons below is valid for some records but not for others. In the On Current event, you can test the current record and set the Enabled property of the button to True or False as appropriate. Because this event fires so often, it can be hard to control and cause performance issues. Use it sparingly.

□ Form—On Delete: This event fires after each record is deleted, but before the delete is actually finalized. This allows you to ask an Are You Sure message and cancel the delete of just that record if the user wants to. Use this in conjunction with the Before Delete Confirm event.

□ Form—Before Delete Confirm: This event fires before a group of deletes is finalized. If you cancel this event, none of the records are actually deleted. This event also has a Response parameter; it can be used to suppress the normal Access message asking the user if they want to delete the group of records.

0 0

Post a comment