InsertText Method

Microsoft Access Visual Basic

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

expression.InsertText(Text)

expression    Required. An expression that returns one of the above objects.

Text   Required String. The text to be inserted into the module.

ShowInsertText method as it applies to the Application object.

The InsertText method inserts a specified string of text into an application.

expression.InsertText(Text, ModuleName)

expression    Required. An expression that returns one of the above objects.

Text   Required String. The text to be inserted into the module.

ModuleName   Required String. The name of the module for the application.

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.

Example

ShowAs it applies to the Module object.

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