InsertText Method

Applies To   Form object, Module object, Report object.

Description

The InsertText method inserts a specified string of text into a standard module or a class module.

Syntax

object.InsertText text

The InsertText method has the following arguments.

Argument

Description

object

A Module object.

text

The text to be inserted into the module.


Remarks   When you insert a string by using the InsertText method, Microsoft Access places the new text at the end of the module, after all other procedures.

To add multiple lines, include the intrinsic constant vbCrLf at the desired line breaks within the string that makes up the text argument. This constant forces a carriage return and line feed.

To specify at which line the text is inserted, use the InsertLines method. To insert code into the Declarations section of the module, use the InsertLines method rather than the InsertText method.

Note In previous versions of Microsoft Access, the InsertText method was a method of the Application object. You can still use the InsertText method of the Application object, but it's recommended that you use the InsertText method of the Module object instead.

See Also   InsertLines method, Module property.

Example

The following example inserts a string of text into a standard module:

Function InsertProc(strModuleName) As Boolean
    Dim mdl As Module, strText As String

    On Error GoTo Error_InsertProc
    ' Open module.
    DoCmd.OpenModule strModuleName
    ' Return reference to Module object.
    Set mdl = Modules(strModuleName)
    ' Initialize string variable.
    strText = "Sub DisplayMessage()" & vbCrLf _
        & vbTab & "MsgBox ""Wild!""" & vbCrLf _
        & "End Sub"
    ' Insert text into module.
    mdl.InsertText strText
    InsertProc = True

Exit_InsertProc:
    Exit Function

Error_InsertProc:
    MsgBox Err & ": " & Err.Description
    InsertProc = False
    Resume Exit_InsertProc
End Function