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:

  1. Click the object for which you want to define an event handler.

  2. Either select View, Edit Code or double-click the object. (You can also right-click the object and select Edit Code from the shortcut menu.) VBA displays the code module for the object, as shown in Figure 13.4.

    Figure 13.4. For each event, VBA defines a mini procedure. You define the procedure by entering code into this stub.

    graphics/13fig04.jpg

  3. Use the procedure drop-down list (the one on the right) to select the event you want to work with.

  4. Enter the rest of the procedure code between the Sub and End Sub statements.



Absolute Beginner's Guide to VBA
Absolute Beginners Guide to VBA
ISBN: 0789730766
EAN: 2147483647
Year: 2003
Pages: 146

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net