Create a Visual Basic procedure

Microsoft Office Access 2003

procedures, you can customize the way the tables, forms, reports, and queries in your database work together. There are several types of procedures. You can create an event procedure by adding code to an event on a form or report. You also can create your own Function procedures or Sub procedures in standard modules, or in class modules (which include form and report modules).

ShowCreate a custom function

  1. To open a module, do one of the following:

    To open a new standard module, in the Database window, click Modules Button image under Objects, and then click the New button on the Database window toolbar.

    To open an existing standard module, click Modules Button image under Objects, select the module you want to open, and then click Design.

    To open a form module or report module, open the form or report in Design view, and then click Code Button image on the toolbar.

    To open a new class module that isn't associated with a form or report, in the Database window click Class Module on the Insert menu.

    To open an existing class module, in the Database window, click Modules Button image under Objects, select the module you want to open, and then click the Design button on the Database window toolbar.

  2. Declare the function by typing the Function statement.
  3. Type a function name immediately followed by any function arguments in parentheses. For example, the following declaration for the IsLoaded function specifies strFormName as an argument:
    Function IsLoaded (strFormName As String) As Boolean
    						
  4. Add the Microsoft Visual Basic code that performs the operation or calculation that you want the function to perform.

ShowCreate a custom Sub procedure

  1. To open a module, do one of the following:

    To open a new standard module, in the Database window, click Modules Button image under Objects, and then click New on the Database window toolbar.

    To open an existing standard module, click Modules Button image under Objects, select the module you want to open, and then click Design.

    To open a form module or report module, open the form or report in Design view, and then click Code Button image on the toolbar.

    To open a new class module that isn't associated with a form or report, in the Database window click Class Module on the Insert menu.

    To open an existing class module, in the Database window, click Modules Button image, select the module you want to open, and then click Design on the Database window toolbar.

  2. Declare the procedure by typing the Sub statement.
  3. Type a procedure name, immediately followed by any arguments in parentheses. For example, the following declaration for the ShowEvent Sub procedure specifies EventName as an argument:
    Sub ShowEvent(EventName As String)
    						
  4. Add the Microsoft Visual Basic code that performs the operation that you want the procedure to perform.

ShowCreate an event procedure

You can set an event property for a form, report, or control to [Event Procedure] to run code in response to an event. Microsoft Access creates the event procedure template for you. You can then add the code you want to run in response to the particular event.

  1. Open a form or report in Design view.
  2. Display the property sheet for the form or report, or for 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 Button image 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.

ShowCreate a class module that is not associated with a form or report

  1. In the Database window or in the Microsoft Visual Basic Editor, click Class Module on the Insert menu.

    An empty class module appears in the Visual Basic Editor.

  2. Add any declarations and procedures that you want to the module.
  3. To save the module, click Save Button image on the toolbar, and specify a name for the class module in the Save As dialog box.