편집

다음을 통해 공유


Module.Find method (Access)

Finds specified text in a standard module or class module.

Syntax

expression.Find (Target, StartLine, StartColumn, EndLine, EndColumn, WholeWord, MatchCase, PatternSearch)

expression A variable that represents a Module object.

Parameters

Name Required/Optional Data type Description
Target Required String The text that you want to find.
StartLine Required Long The line on which to begin searching. If a match is found, the value of the StartLine argument is set to the line on which the beginning character of the matching text is found.
StartColumn Required Long The column on which to begin searching. Each character in a line is in a separate column, beginning with zero on the left side of the module. If a match is found, the value of the StartColumn argument is set to the column in which the beginning character of the matching text is found.
EndLine Required Long The line on which to stop searching. If a match is found, the value of the EndLine argument is set to the line on which the ending character of the matching text is found.
EndColumn Required Long The column on which to stop searching. If a match is found, the value of the EndColumn argument is set to the column in which the beginning character of the matching text is found.
WholeWord Optional Boolean True results in a search for whole words only. The default is False.
MatchCase Optional Boolean True results in a search for words with case matching the Target argument. The default is False.
PatternSearch Optional Boolean True results in a search in which the Target argument may contain wildcard characters such as an asterisk (*) or a question mark (?). The default is False.

Return value

Boolean

Remarks

The Find method searches for the specified text string in a Module object. If the string is found, the Find method returns True.

To determine the position in the module at which the search text was found, pass empty variables to the Find method for the StartLine, StartColumn, EndLine, and EndColumn arguments. If a match is found, these arguments will contain the line number and column position at which the search text begins (StartLine, StartColumn) and ends (EndLine, EndColumn).

For example, if the search text is found on line 5, begins at column 10, and ends at column 20, the values of these arguments will be StartLine = 5, StartColumn = 10, EndLine = 5, EndColumn = 20.

Example

The following function finds a specified string in a module and replaces the line that contains that string with a new specified line.

Function FindAndReplace(strModuleName As String, _ 
 strSearchText As String, _ 
 strNewText As String) As Boolean 
 Dim mdl As Module 
 Dim lngSLine As Long, lngSCol As Long 
 Dim lngELine As Long, lngECol As Long 
 Dim strLine As String, strNewLine As String 
 Dim intChr As Integer, intBefore As Integer, _ 
 intAfter As Integer 
 Dim strLeft As String, strRight As String 
 
 ' Open module. 
 DoCmd.OpenModule strModuleName 
 ' Return reference to Module object. 
 Set mdl = Modules(strModuleName) 
 
 ' Search for string. 
 If mdl.Find(strSearchText, lngSLine, lngSCol, lngELine, _ 
 lngECol) Then 
 ' Store text of line containing string. 
 strLine = mdl.Lines(lngSLine, Abs(lngELine - lngSLine) + 1) 
 ' Determine length of line. 
 intChr = Len(strLine) 
 ' Determine number of characters preceding search text. 
 intBefore = lngSCol - 1 
 ' Determine number of characters following search text. 
 intAfter = intChr - CInt(lngECol - 1) 
 ' Store characters to left of search text. 
 strLeft = Left$(strLine, intBefore) 
 ' Store characters to right of search text. 
 strRight = Right$(strLine, intAfter) 
 ' Construct string with replacement text. 
 strNewLine = strLeft & strNewText & strRight 
 ' Replace original line. 
 mdl.ReplaceLine lngSLine, strNewLine 
 FindAndReplace = True 
 Else 
 MsgBox "Text not found." 
 FindAndReplace = False 
 End If 
 
Exit_FindAndReplace: 
 Exit Function 
 
Error_FindAndReplace: 
 
MsgBox Err & ": " & Err.Description 
 FindAndReplace = False 
 Resume Exit_FindAndReplace 
End Function

Support and feedback

Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.