The Form Module and Event Handling

 < Day Day Up > 

All the procedures you've seen so far in this chapter have been located in standard modules. But as you've already seen several times in this book, VBA code can also be associated directly with a form. Every form in your Access application can have its own form module. The form module is saved as a part of the form so that if (for example) you copy the form to another database, the form comes along with it.

One type of code that is stored within the form module is the event procedures for the form. These procedures are called when something happens to the form or to a control on the form. For example, clicking a button on a form calls the event procedure associated with the button's Click event. Here's how you can add a Close button to the Clients form:

  1. Open the Clients form in Design view.

  2. Add a command button control to the form. Name the control cmdClose and set its caption to Close.

  3. Set the On Click property of the button to [Event Procedure] and click the builder button next to the property to open the form's module.

  4. Enter the code to close the form:


     Private Sub cmdClose_Click()   DoCmd.Close End Sub 

  5. Save the form and test the button.

     < Day Day Up > 

    Automating Microsoft Access with VBA
    Automating Microsoft Access with VBA
    ISBN: 0789732440
    EAN: 2147483647
    Year: 2003
    Pages: 186 © 2008-2017.
    If you may any questions please contact us: