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.
A procedure is a unit of Microsoft Visual Basic code. A procedure contains a series of statements and methods that perform an operation or calculate a value. For example, the following event procedure 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 and Function 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 or report module 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 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.
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. 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 or report module, or in a standard module. You can't use a function in a class module that isn't associated with a form or report as a form or report property setting.
Example 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.
To make an event procedure run when a user clicks the command button, click the OnClick property of the command...
... click the Build button, and then click Code Builder to add Visual Basic code to the event procedure.
The ProductDetails_Click event procedures uses the OpenForm method to open the Product Details form.