【问题标题】:Find function in Form Code Module在表单代码模块中查找函数
【发布时间】:2012-03-08 03:56:48
【问题描述】:

我要调查许多 Access 应用程序,如果可能的话,编写脚本会让我的生活更轻松。

我的主要目标是在 MS Access 表单代码/模块中搜索某些单词(例如 dog、cat、cow 等)并返回包含该单词的模块名称。 (或者至少返回,如果它存在与否)

据我了解,内置的 Find 功能不会查找多个单词,而只会查找单个单词。 我不希望将每个单词都放在那里并进行查找,因为这会消耗大量时间。

你会如何解决这个问题?

如下所述,可以使用 Find 功能(Module.Find)搜索普通模块,但它似乎与 Form.Module 不同。请帮忙!

【问题讨论】:

    标签: ms-access search text


    【解决方案1】:

    您可以使用类似的东西,但它不会在无人看管的情况下运行,因为您可能会遇到密码等各种问题。

    重写

    这将搜索表单和报告代码和模块,包括类模块,但不会搜索 cmets。

    Sub SearchAllCode()
    Dim ap As New Access.Application
    Dim sfile As String, afind As Variant
    Dim mdlname As String, mdl As Object
    Dim prcname As String
    Dim lsline As Long, lscol As Long
    Dim leline As Long, lecol As Long
    Dim sline As String, r As Long
    Dim i, j
    
    ap.Visible = True
    
    afind = Split("msgbox,chair,ombo,Visible", ",")
    sfile = Dir("Z:\Docs\*.accdb")
    
    Do While sfile <> vbNullString
        ap.OpenCurrentDatabase "Z:\Docs\" & sfile, False, "pass"
    
        For i = 1 To ap.VBE.ActiveVBProject.VBComponents.Count
    
            Set mdl = ap.VBE.ActiveVBProject.VBComponents(i).CodeModule 
    
            For j = 0 To UBound(afind)
                leline = mdl.CountOfLines
                ''object.Find(target, startline, startcol, endline, endcol 
                ''[, wholeword] [, matchcase] [, patternsearch]) As Boolean
                ''The default is false for the three optional parameters.
                ''Finds first occurrence only
                If mdl.Find(afind(j), lsline, lscol, leline, lecol) Then
    
                    sline = mdl.Lines(lsline, Abs(leline - lsline) + 1)
                    prcname = mdl.ProcOfLine(lsline, r)
    
                    Debug.Print mdl.Name
                    Debug.Print prcname
                    Debug.Print lsline
                    Debug.Print sline
                End If
            Next
        Next
        ap.CloseCurrentDatabase
        sfile = Dir
    Loop
    ap.Quit
    End Sub
    

    这是一种替代搜索,但它不会为您提供在找到该行后操作代码的方法。

    Sub AlternativeSearch()
    Dim ap As New Access.Application
    Dim sfile As String, afind As Variant
    Dim mdl As Object
    Dim modtext As String, modarray As Variant
    Dim leline As Long
    Dim i, j, k
    
    
    ap.Visible = True
    
    afind = Split("msgbox,chair,ombo,Visible", ",")
    sfile = Dir("Z:\Docs\*.accdb")
    
    Do While sfile <> vbNullString
        ap.OpenCurrentDatabase "Z:\Docs\" & sfile, False, "pass"
    
        For i = 1 To ap.VBE.ActiveVBProject.VBComponents.Count
    
            Set mdl = ap.VBE.ActiveVBProject.VBComponents(i).codemodule 'ap.Modules(mdlname)
            leline = mdl.CountOfLines
            modtext = mdl.Lines(1, leline)
    
            For j = 0 To UBound(afind)
                If InStr(modtext, afind(j)) > 0 Then
                    Debug.Print "****" & afind(j) & " found in " & mdl.Name
                    modarray = Split(modtext, vbCrLf)
                    For k = 0 To UBound(modarray)
                        If InStr(modarray(k), afind(j)) > 0 Then
                            Debug.Print k
                            Debug.Print modarray(k)
                        End If
                    Next
                End If
            Next
        Next
        ap.CloseCurrentDatabase
        sfile = Dir
    Loop
    ap.Quit
    End Sub
    

    【讨论】:

    • 感谢您的回复!该代码适用于模块中的代码,但有没有办法搜索表单对象代码?
    猜你喜欢
    • 2015-04-23
    • 2019-04-28
    • 1970-01-01
    • 2020-04-30
    • 2021-06-19
    • 2018-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多