Skip to Content
Course content

6.1. Using Modules and Procedures

In Access VBA, Modules and Procedures are fundamental components that allow you to organize and execute your code. Modules are containers for procedures (which include functions and subroutines) that help structure your code, making it more modular, reusable, and easier to manage.

1. What Are Modules?

A Module in Access VBA is a container that holds procedures (such as subroutines and functions). Modules are used to store code that can be accessed and executed from anywhere within the database. There are two types of modules in Access:

a. Standard Modules

  • These are general-purpose modules used for storing reusable code such as functions, subroutines, and variables that can be accessed from multiple forms or reports.
  • They are often used for tasks like data validation, calculations, or custom functions that are not tied to any particular form or report.

b. Class Modules

  • These are used to define custom objects and their properties, methods, and events, typically for more advanced, object-oriented programming.
  • Class modules allow for encapsulating code specific to certain forms or reports.

2. What Are Procedures?

Procedures in VBA are blocks of code designed to perform specific tasks. There are two main types of procedures:

a. Subroutines (Sub Procedures)

  • A Subroutine (or Sub) is a procedure that performs a task but does not return a value. It is invoked with a Call statement or by simply calling its name.
  • Example: A subroutine that opens a form when called.
Sub OpenForm()
    DoCmd.OpenForm "CustomersForm"
End Sub

b. Functions

  • A Function is similar to a Subroutine but it returns a value. Functions are useful when you need to perform a task and then return a result (e.g., calculation or decision-making).
  • Example: A function that returns the sum of two numbers.
Function AddNumbers(num1 As Double, num2 As Double) As Double
    AddNumbers = num1 + num2
End Function

3. Creating and Using Modules in Access VBA

To create a module in Access:

  1. Open the VBA Editor by pressing Alt + F11.
  2. In the VBA editor, go to Insert > Module to create a standard module.
  3. Write your subroutines or functions inside the module.

Example of a Module with Subroutines and Functions

' Standard Module: Module1

' Subroutine to open a form
Sub OpenCustomerForm()
    DoCmd.OpenForm "CustomerForm"
End Sub

' Function to calculate total price
Function CalculateTotal(price As Double, quantity As Integer) As Double
    CalculateTotal = price * quantity
End Function
  • The OpenCustomerForm subroutine can be called to open the "CustomerForm".
  • The CalculateTotal function can be used to calculate the total price by passing the price and quantity parameters.

Calling Procedures from Other Modules or Forms

You can call subroutines or functions defined in modules from other parts of your Access application, such as from forms, reports, or other modules.

  • Calling a Subroutine:
Call OpenCustomerForm ' Calls the subroutine to open the customer form
  • Calling a Function:
Dim total As Double
total = CalculateTotal(100, 2) ' Calculates total price for price = 100 and quantity = 2
MsgBox "The total price is " & total

4. Benefits of Using Modules and Procedures

  • Code Reusability: By storing procedures in modules, you can reuse them throughout the application without duplicating code.
  • Better Code Organization: Modules help to organize code logically, keeping related procedures together. This makes your codebase cleaner and easier to maintain.
  • Encapsulation: You can encapsulate logic in functions and subroutines, which helps to isolate tasks and makes debugging easier.
  • Improved Maintainability: When code is modular, it’s easier to make changes. For example, updating a calculation in a function in one place updates the logic wherever the function is used.

5. Types of Procedures

a. Event Procedures

  • These are procedures that are automatically triggered by events like a button click, form load, or when a value changes in a control.
  • Example: Button Click Event Procedure
Private Sub btnSave_Click()
    MsgBox "Data Saved Successfully"
End Sub

b. Custom Procedures

  • These are procedures written by the programmer to perform specific tasks, like data validation or processing.
  • Example: Data Validation Function
Function ValidateEmail(email As String) As Boolean
    If InStr(email, "@") > 0 Then
        ValidateEmail = True
    Else
        ValidateEmail = False
    End If
End Function

6. Best Practices for Using Modules and Procedures

  • Use Descriptive Names: Name your modules, functions, and subroutines descriptively, so it’s clear what they do. For example, use ValidateEmail rather than just Validate.
  • Modularize Code: Break down complex tasks into smaller, manageable subroutines or functions. This makes it easier to understand, test, and debug.
  • Avoid Global Variables: While you can declare variables globally, it’s best practice to minimize this and use local variables within procedures when possible to avoid unintended side effects.
  • Comment Your Code: Add comments to your procedures to explain what they do, especially if the logic is complex.

7. Conclusion

Modules and procedures are powerful tools for organizing and managing your code in Access VBA. By using modules to group related code and defining subroutines and functions for specific tasks, you can create cleaner, more maintainable applications. Leveraging these concepts will help you write efficient, reusable code and improve the overall structure of your Access database applications.

Commenting is not enabled on this course.