ms-access – 在表单代码模块中查找功能

前端之家收集整理的这篇文章主要介绍了ms-access – 在表单代码模块中查找功能前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我将调查许多Acccess应用程序,如果可能的话,它将使我的生活更容易制作脚本.

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

据我了解,内置的Find函数不会查找多个单词,只需单个单词.
我不希望把每个字放在那里,然后做一个全部查找,因为这将耗费大量时间.

你会如何解决这个问题?

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

解决方法

你可以使用这样的东西,但它不会无人看管,因为你可能会遇到各种各样的密码等问题.

改写

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

  1. Sub SearchAllCode()
  2. Dim ap As New Access.Application
  3. Dim sfile As String,afind As Variant
  4. Dim mdlname As String,mdl As Object
  5. Dim prcname As String
  6. Dim lsline As Long,lscol As Long
  7. Dim leline As Long,lecol As Long
  8. Dim sline As String,r As Long
  9. Dim i,j
  10.  
  11. ap.Visible = True
  12.  
  13. afind = Split("msgBox,chair,ombo,Visible",",")
  14. sfile = Dir("Z:\Docs\*.accdb")
  15.  
  16. Do While sfile <> vbNullString
  17. ap.OpenCurrentDatabase "Z:\Docs\" & sfile,False,"pass"
  18.  
  19. For i = 1 To ap.VBE.ActiveVBProject.VBComponents.Count
  20.  
  21. Set mdl = ap.VBE.ActiveVBProject.VBComponents(i).CodeModule
  22.  
  23. For j = 0 To UBound(afind)
  24. leline = mdl.CountOfLines
  25. ''object.Find(target,startline,startcol,endline,endcol
  26. ''[,wholeword] [,matchcase] [,patternsearch]) As Boolean
  27. ''The default is false for the three optional parameters.
  28. ''Finds first occurrence only
  29. If mdl.Find(afind(j),lsline,lscol,leline,lecol) Then
  30.  
  31. sline = mdl.Lines(lsline,Abs(leline - lsline) + 1)
  32. prcname = mdl.ProcOfLine(lsline,r)
  33.  
  34. Debug.Print mdl.Name
  35. Debug.Print prcname
  36. Debug.Print lsline
  37. Debug.Print sline
  38. End If
  39. Next
  40. Next
  41. ap.CloseCurrentDatabase
  42. sfile = Dir
  43. Loop
  44. ap.Quit
  45. End Sub

这是一个替代搜索,但一旦找到该行,它就不会为您提供操作代码方法.

  1. Sub AlternativeSearch()
  2. Dim ap As New Access.Application
  3. Dim sfile As String,afind As Variant
  4. Dim mdl As Object
  5. Dim modtext As String,modarray As Variant
  6. Dim leline As Long
  7. Dim i,j,k
  8.  
  9.  
  10. ap.Visible = True
  11.  
  12. afind = Split("msgBox,"pass"
  13.  
  14. For i = 1 To ap.VBE.ActiveVBProject.VBComponents.Count
  15.  
  16. Set mdl = ap.VBE.ActiveVBProject.VBComponents(i).codemodule 'ap.Modules(mdlname)
  17. leline = mdl.CountOfLines
  18. modtext = mdl.Lines(1,leline)
  19.  
  20. For j = 0 To UBound(afind)
  21. If InStr(modtext,afind(j)) > 0 Then
  22. Debug.Print "****" & afind(j) & " found in " & mdl.Name
  23. modarray = Split(modtext,vbCrLf)
  24. For k = 0 To UBound(modarray)
  25. If InStr(modarray(k),afind(j)) > 0 Then
  26. Debug.Print k
  27. Debug.Print modarray(k)
  28. End If
  29. Next
  30. End If
  31. Next
  32. Next
  33. ap.CloseCurrentDatabase
  34. sfile = Dir
  35. Loop
  36. ap.Quit
  37. End Sub

猜你在找的HTML相关文章