Create an event procedure

Create an event procedure

Microsoft Access makes it easy to run code in response to an action performed by the user. When you set an event property for a form, report, or control to [Event Procedure], Microsoft Access creates the event procedure template for you. All you have to do is add the code you want to run in response to a particular event on the form, report, or control. For example, you can have code run when a user clicks a command button or changes data.

  1. Open a form or report in form or report Design view.

  2. Display the property sheet for the form, report, or a section or control on the form or report.

  3. Click the Event tab.

  4. Click the event property for the event that you want to trigger the procedure. For example, to display the event procedure for the Change event, click the OnChange property.

  5. Click Build next to the property box to display the Choose Builder dialog box.

  6. Double-click Code Builder to display the event procedure Sub and End Sub statements in the form module or report module. These statements define, or declare, the event procedure.

    Microsoft Access automatically declares event procedures for each object and event in a form or report module by using the Private keyword to indicate that the procedure can be accessed only by other procedures in that module.

  7. Add the code to the event procedure that you want to run when the event occurs. For example, to produce a sound through the computer's speaker when data in the CompanyName text box changes, add a Beep statement to the CompanyName_Change event procedure, as follows:
    Private Sub CompanyName_Change()
    
        Beep
    
    End Sub

    The event procedure runs each time the Change event occurs for the object.