Module Object

Description

A Module object refers to a standard module or a class module.

Remarks

Microsoft Access includes class modules which are not associated with any object, and form modules and report modules, which are associated with a form or report.

To determine whether a Module object represents a standard module or a class module from code, check the Module object's Type property.

The Modules collection contains all open Module objects, regardless of their type. Modules in the Modules collection can be compiled or uncompiled.

To return a reference to a particular standard or class Module object in the Modules collection, use any of the following syntax forms.

Syntax

Description

Modules!modulename

The modulename argument is the name of the Module object.

Modules("modulename")

The modulename argument is the name of the Module object.

Modules(index)

The index argument is the numeric position of the object within the collection.


The following example returns a reference to a standard Module object and assigns it to an object variable:

Dim mdl As Module
Set mdl = Modules![Utility Functions]
Note that the brackets enclosing the name of the Module object are necessary only if the name of the Module includes spaces.

The next example returns a reference to a form Module object and assigns it to an object variable:

Dim mdl As Module
Set mdl = Modules!Form_Employees
To refer to a specific form or report module, you can also use the Form or Report object's Module property:

Forms!formname.Module

The following example also returns a reference to the Module object associated with an Employees form and assigns it to an object variable:

Dim mdl As Module
Set mdl = Forms!Employees.Module
Once you've returned a reference to a Module object, you can set or read its properties and apply its methods.

Properties

Application property, CountOfDeclarationLines property, CountOfLines property, Lines property, Parent property, ProcBodyLine property, ProcCountLines property, ProcOfLine property, ProcStartLine property, Type property.

Methods

AddFromFile method (Module object), AddFromString method, CreateEventProc method, DeleteLines method, Find method, InsertLines method, InsertText method, ReplaceLine method.

Events

Initialize event, Terminate event.

See Also   Modules collection.

Example

The following example returns a reference to a Module object in the Modules collection and prints the number of lines of code in the module:

Function LinesInModule(strModuleName As String) As Long
    Dim mdl As Module

    On Error GoTo Error_LinesInModule
    ' Open module.
    DoCmd.OpenModule strModuleName
    ' Return reference to Module object.
    Set mdl = Modules(strModuleName)
    ' Return number of lines in module.
    LinesInModule = mdl.CountOfLines

Exit_LinesInModule:
    Exit Function

Error_LinesInModule:
    MsgBox Err & ": " & Err.Description
    ' If function fails, return -1.
    LinesInModule = -1
    Resume Exit_LinesInModule
End Function