如果您的兴趣是在 Access 数据库文件中搜索代码模块,则可以使用 VBE 对象模型。ActiveVBProject
此示例在当前数据库的所有模块中搜索一个单词。如果数据库包含多个 VBProject,您可以枚举 VBProjects 集合并按名称一次搜索一个项目:
For Each objComponent In Application.VBE.VBProjects(ProjName).VBComponents
或者,如果您更喜欢按编号而不是名称来引用项目,请注意编号以 1 而不是 0 开头。
Public Sub findWordInModules(ByVal pSearchWord As String)
'Dim objComponent As VBComponent
' VBComponent requires reference to Microsoft Visual Basic
' for Applications Extensibility; use late binding instead:
Dim objComponent As Object
Dim strMessage As String
Dim strModuleList As String
strModuleList = vbNullString
For Each objComponent In Application.VBE.ActiveVBProject.VBComponents
If objComponent.CodeModule.Find(pSearchWord, 1, 1, -1, -1) = True Then
strModuleList = strModuleList & "; " & objComponent.Name
End If
Next objComponent
strMessage = "Text '" & pSearchWord & "' found in "
If Len(strModuleList) > 0 Then
strMessage = strMessage & "modules: " & Mid(strModuleList, 3)
Else
strMessage = strMessage & "no modules"
End If
Debug.Print strMessage
End Sub
查看该Find
方法的 Access 帮助主题;您可能更喜欢与我使用的不同的选项。
如果您想定位多个 db 文件并搜索每个文件中的模块,您可以使用该OpenDatabase
方法自动执行此操作。我将把这部分的细节留给你。