InsertText Method

Description

You can use the InsertText method from Visual Basic code to add text to another module.

For example, suppose you are building a wizard that creates a new form and adds code to that form’s module. You can use the InsertText method to add the specified text to the module when the wizard is run.

Setting

The InsertText method uses two syntax forms.

Syntax 1

object.Module.InsertText text

You can use this syntax form to insert text into a form module or a report module. This syntax form uses the following arguments.

Argument

Description

object

A Form or Report object containing the module to which you want to add text.

Module

The property that specifies a form or report module.

text

A string expression that specifies the text to insert.


Syntax 2

application.InsertText text, modulename

You can use this syntax form to insert text into a standard module. This syntax form uses the following arguments.

Argument

Description

application

The Application object.

text

The text to insert.

modulename

The name of the standard module into which text is to be inserted.


Remarks

Use the InsertText method in a custom wizard to add code to a module.

If you are adding text to a form or report module, the InsertText method is available only in form Design view or report Design view. If you try to add text to the module of a form or report that is open in any view but Design view, Microsoft Access generates a run-time error.

If a procedure in the module to which you are adding text is suspended, Microsoft Access generates a run-time error.

See Also

Module Property.

Example

The following example inserts code in the form module associated with a form called Orders. The code sounds a beep when the form is opened.


Sub AddSub()
    Dim strFormOpenCode As String

    strFormOpenCode = "Sub Form_Open(Cancel As Integer)" & vbCrLf _
        & "Beep" & vbCrLf & "End Sub"
    Forms!Orders.Module.InsertText strFormOpenCodeSub

The next example inserts code into a standard module called PublicFunctions.


Sub AddDeclaration()
    Dim strText As String

    strText = "Public varName"
    Application.InsertText strText, "PublicFunctions"Sub