Handling Form Events
An event-driven language is one in which code can respond to specific events, such as a user clicking a command button or selecting an item from a list. The procedure can then take appropriate action, whether it's validating the user's input or asking for confirmation of the requested action. A form responds to more than 20 separate events, including activating and deactivating the form, displaying the form, clicking the form, and resizing the form.
For each event associated with an object, VBA has set up mini procedures called event handlers. These procedures are really just Sub and End Sub statements. You process the event by filling in your own VBA code between these statements. Here are the steps to follow:
- Click the object for which you want to define an event handler.
- Either select View, Code or double-click the object. (You can also press F7 or rightclick the object and then click select View Code in the shortcut menu.) VBA displays the code module for the object.
- Use the procedure drop-down list (the one on the right) to select the event with which you want to work.
- Enter the rest of the procedure code between the Sub and End Sub statements.