Export (0) Print
Expand All

Module.InsertText Method (Access)

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

expression .InsertText(Text)

expression A variable that represents a Module object.

Parameters

Name

Required/Optional

Data Type

Description

Text

Required

String

The text to be inserted into the module.

Return Value

Nothing

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 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.

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
Show:
© 2015 Microsoft