在表单代码模块中查找功能 [英] Find function in Form Code Module

查看:74
本文介绍了在表单代码模块中查找功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要研究许多Acccess应用程序,如果可能的话,这将使我的生活变得更容易.

I am to investigate many Acccess applications and it would make my life easier to make a script, if possible.

我的主要目标是通过MS Access表单代码/模块搜索某些单词(例如狗,猫,牛等),然后返回包含该单词的模块名称. (或至少返回是否存在)

My main goal is to search through MS Access form code/module for certain words (eg dog, cat, cow etc) and return the module name which contains that word. (or at least return if it exists or not)

据我了解,内置的Find函数不会查找多个单词,而只会查找单个单词. 我不希望将每个单词都放在那里并进行查找,因为那样会浪费很多时间.

As I understand it, the inbuilt Find function does not look for multiple words, just single word. I do not wish to put each word in there and do a Find all as that will consume lot of time.

您将如何解决这个问题?

How would you tackle this problem?

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

As answered below, normal Modules can be searched using Find function (Module.Find) but it seems it is different to Form.Module. Please help!

推荐答案

您可以使用类似的方法,但是它不会在无人值守的情况下运行,因为您可能会遇到各种密码等问题.

You could use something like this, but it will not run unattended, because you are likely to get all sorts of problems with passwords and such like.

重写

这将搜索表单并报告代码和模块,包括includimg类模块,但不会搜索注释.

This will search form and report code and modules, includimg class modules, but will not search comments.

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

这是另一种搜索方法,但是一旦找到该行,它就不会为您提供处理代码的方法.

Here is an alternative search, but it does not give you the means of manipulating the code, once the line is found.

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

这篇关于在表单代码模块中查找功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆