Run Visual Basic code

You run Microsoft Visual Basic (Microsoft Visual Basic: A high-level, visual-programming version of Basic. Visual Basic was developed by Microsoft for building Windows-based applications.) code in Microsoft Access by running a Sub procedure (Sub procedure: A Visual Basic for Applications (VBA) procedure that carries out an operation. Unlike a Function procedure, a Sub procedure does not return a value.) or Function procedure (Function procedure: A procedure that returns a value and that can be used in an expression. You declare a function with the Function statement and end it with the End Function statement.). Procedures contain a series of statements (statement: A syntactically complete unit that expresses one specific kind of operation, declaration, or definition. A statement is usually on one line in a procedure or Declarations section, but two or more can be on a line separated by a colon (:).) and methods (method: A procedure similar to a statement or function that operates on specific objects. For example, you can apply the Print method to the Debug object to transfer printed output to the Immediate window.) that perform an operation or calculate a value.

Procedures are stored in units called modules (module: A collection of declarations, statements, and procedures stored together as one named unit. There are two types of modules: standard modules and class modules.). However, you don't run a module; rather, you call the procedures from expressions (expression: Any combination of mathematical or logical operators, constants, functions, and names of fields, controls, and properties that evaluates to a single value. Expressions can perform calculations, manipulate characters, or test data.), other procedures, or macros (macro: An action or set of actions that you can use to automate tasks.).Event procedures (event procedure: A procedure that is automatically executed in response to an event initiated by the user or program code, or that is triggered by the system.) in form (form module: A module that includes Visual Basic for Applications (VBA) code for all event procedures triggered by events occurring on a specific form or its controls.) or report modules (report module: A module that includes Visual Basic for Applications (VBA) code for all event procedures triggered by events occurring on a specific report or its controls.) 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.

For example, you can use a function in an expression that defines a calculated field (calculated field: A field, defined in a query, that displays the result of an expression rather than displaying stored data. The value is recalculated each time a value in the expression changes.) 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.

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.

Applies to:
Access 2003