Run Visual Basic code
You run Microsoft Visual Basic code in Microsoft Access by running a Sub procedure or Function procedure. Procedures contain a series of statements and methods that perform an operation or calculate a value.
Procedures are stored in units called modules. However, you don't run a module; rather, you call the procedures from expressions, other procedures, or macros.Event procedures in form or report modules also run in response to user actions.
To run Visual Basic code in Microsoft Access, do one of the following:
- Create an event procedure. When the user performs an action that causes that event, the event procedure runs.
For example, you can add code to the Click event procedure of a command button that opens a form when a user clicks the button.
- Use a function in an expression or in the Immediate window of the Microsoft Visual Basic Editor.
For example, you can use a function in an expression that defines a calculated field in a form, report, or query. You can use expressions as property settings in queries and filters, in macros and actions, in Visual Basic statements and methods, or in SQL statements.
- Call a Sub procedure in another procedure or in the Immediate window of the Visual Basic Editor.
If you have code that you run frequently, you can put it in a Sub procedure. Rather than repeating the Visual Basic code that performs the operation in each procedure, you write it once in the common procedure and then call the common procedure each time you want to perform the operation.
- Run a procedure that does not take arguments. In the Code window of the Visual Basic Editor, place the cursor in the procedure that you want to run and then click Run Sub/UserForm on the Run menu.
- Carry out the RunCode action in a macro.
You can use the RunCode action to run a built-in Visual Basic function or a function you've created. To run a Sub procedure or an event procedure, create a function that calls the Sub procedure or event procedure, and then use the RunCode action to run the function.