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.