What is a procedure?

What is a procedure?

A procedure is a unit of 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:

Here is an example of a Function procedure, FirstOfNextMonth, that 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.

Both Sub and Function procedures can accept arguments. For more information on using arguments in Visual Basic, click .