- To open a module, do one of the following:
To open a new standard module, in the Database window, click Modules under Objects, and then click the New button on the Database window toolbar.
To open an existing standard module, click Modules 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 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 under Objects, select the module you want to open, and then click the Design button on the Database window toolbar.
- Declare the function by typing the Function statement.
- 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
- Add the Microsoft Visual Basic code that performs the operation or calculation that you want the function to perform.
- To open a module, do one of the following:
To open a new standard module, in the Database window, click Modules under Objects, and then click New on the Database window toolbar.
To open an existing standard module, click Modules 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 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 , select the module you want to open, and then click Design on the Database window toolbar.
- Declare the procedure by typing the Sub statement.
- 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)
- Add the Microsoft Visual Basic code that performs the operation that you want the procedure to perform.
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.
- Open a form or report in Design view.
- Display the property sheet for the form or report, or for a section or control on the form or report.
- Click the Event tab.
- 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.
- Click Build next to the property box to display the Choose Builder dialog box.
- 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.
- 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.
Create a class module that is not associated with a form or report
- 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.
- Add any declarations and procedures that you want to the module.
- To save the module, click Save on the toolbar, and specify a name for the class module in the Save As dialog box.