Microsoft Office Online
Sign in to My Office Online (What's this?) | Sign in

 
 
Microsoft Office Access
Search
Search
 
Check for updates: (c) Microsoft
Office downloads
 
 
 
Warning: You are viewing this page with an unsupported Web browser. This Web site works best with Microsoft Internet Explorer 6.0 or later, Firefox 1.5, or Netscape Navigator 8.0 or later. Learn more about supported browsers.

Email this linkEmail this link Printer-Friendly VersionPrinter-Friendly Version Bookmark and ShareShare
About events and event procedures
 

Microsoft Visual Basic code is written in units called procedures. A procedure contains a series of Visual Basic statements that perform an operation or calculate a value. An event procedure is a procedure that runs in response to an event initiated by the user or program code, or triggered by the system.

ShowWhat is an event?

An event is a specific action that occurs on or with a certain object. Microsoft Access can respond to a variety of events: mouse clicks, changes in data, forms opening or closing, and many others. Events are usually the result of user action.

By using an event procedure, you can add your own custom response to an event that occurs on a form, report, or control.

ShowWhat is a procedure?

A procedure is a unit of Microsoft Visual Basic (Microsoft Visual Basic: A high-level, visual-programming version of Basic. Visual Basic was developed by Microsoft for building Windows-based applications.) code. A procedure contains a series of statements (statement: A syntactically complete unit that expresses one specific kind of operation, declaration, or definition. A statement is usually on one line in a procedure or Declarations section, but two or more can be on a line separated by a colon (:).) and methods (method: A procedure similar to a statement or function that operates on specific objects. For example, you can apply the Print method to the Debug object to transfer printed output to the Immediate window.) that perform an operation or calculate a value. For example, the following event procedure (event procedure: A procedure that is automatically executed in response to an event initiated by the user or program code, or that is triggered by the system.) uses the OpenForm method to open the Orders form:

Private Sub OpenOrders_Click()

DoCmd.OpenForm "Orders"
End Sub
				

There are two kinds of procedures: Sub procedures (Sub procedure: A procedure that carries out an operation. Unlike a Function procedure, a Sub procedure doesn't return a value. You begin a Sub procedure with a Sub statement and end it with an End Sub statement.) and Function procedures (Function procedure: A procedure that returns a value and that can be used in an expression. You declare a function with the Function statement and end it with the End Function statement.).

ShowSub procedures

Sub procedures perform an operation or series of operations but don't return a value. You can create your own Sub procedures or use the event procedure templates that Microsoft Access creates for you.

Each form and report in your database has a built-in form module (form module: A module that includes code for all event procedures triggered by events occurring on a specific form or its controls.) or report module (report module: A module that includes code for all event procedures triggered by events occurring on a specific report or its controls.) that contains event procedure templates. You can add code that runs in response to events that occur on the form, the report, or the controls (control: A graphical user interface object, such as a text box, check box, scroll bar, or command button, that lets users control the program. You use controls to display data or choices, perform an action, or make the user interface easier to read.) on the form or report. When Microsoft Access recognizes that an event has occurred on a form, report, or control, it automatically runs the event procedure named for the object and event. For example, you can write an event procedure that sets the focus to a Spouse Name text box when the user selects a Married check box.

Many wizards (for example, the Command Button Wizard) that create objects also create event procedures for the object. You can look at these event procedures to see examples of how procedures work.

ShowFunction procedures

Function procedures (often just called functions) return a value, such as the result of a calculation. Microsoft Visual Basic includes many built-in functions; for example, the Now function returns the current date and time. In addition to these built-in functions, you can create your own custom functions.

Because functions return values, you can use them in expressions (expression: Any combination of mathematical or logical operators, constants, functions, and names of fields, controls, and properties that evaluates to a single value. Expressions can perform calculations, manipulate characters, or test data.). You can use functions in expressions in many places in Microsoft Access, including in a Visual Basic statement or method, in many property settings, or in a criteria expression in a filter or query.

The following example of a Function procedure, FirstOfNextMonth, returns the date of the first day of the month following the current date:

Function FirstOfNextMonth()
 FirstOfNextMonth = _
 DateSerial(Year(Now), Month(Now) + 1, 1)
End Function
						

This custom function consists of a single assignment statement that assigns the results of an expression (on the right side of the equal sign) to the name of the function, FirstOfNextMonth (on the left side of the equal sign). The function calculates a result by using the built-in Visual Basic DateSerial, Year, Now, and Month functions.

After you create this function, you can use it in an expression almost anywhere in Microsoft Access. For example, you could specify that a text box display the first day of the month following the current date as its default value by setting the text box control's DefaultValue property to the following expression in the property sheet:

=FirstOfNextMonth()
						

Note  To use a function as a property setting, the function must be in the form (form module: A module that includes code for all event procedures triggered by events occurring on a specific form or its controls.) or report module (report module: A module that includes code for all event procedures triggered by events occurring on a specific report or its controls.), or in a standard module (standard module: A module in which you can place Sub and Function procedures that you want to be available to other procedures throughout your database.). You can't use a function in a class module (class module: A module that can contain the definition for a new object. When you create a new instance of a class, you create the new object. Procedures defined in the module become the properties and methods of the object.) that isn't associated with a form or report as a form or report property setting.

ShowExample of using an event procedure to respond to an event

When you create an event procedure for an object, Microsoft Access adds an event procedure template named for the event and the object to the form or report module. All you need to do is add code that responds in the way you want when the event occurs for the form or report.

Suppose you want a Product Details form to open when you click a Product Details command button on an Orders form. The following example shows how to do this by using an event procedure.

Create a command button event

Callout 1 To make an event procedure run when a user clicks the command button, click the OnClick property of the command...

Callout 2 ... click the Build button, and then click Code Builder to add Visual Basic code to the event procedure.

Place methods in event procedures to carry out actions

Callout 1 The ProductDetails_Click event procedures uses the OpenForm method to open the Product Details form.

advertisement