【问题标题】:Text-search in properties Access objects属性中的文本搜索访问对象
【发布时间】:2011-09-18 12:47:38
【问题描述】:

Access 中有没有办法在对象属性等中搜索特定文本?不仅仅是在 VBA 源代码中。

我问这个是因为如果我更改例如表中字段的名称,我必须检查很多对象属性(记录源、控制源、排序依据,...)。这可以通过跟踪错误或检查表单的每个控件的所有属性来完成,但这需要很多时间。

一个选项是Find and Replace 工具(不错的工具!),但对我来说有点矫枉过正。我不需要文本替换(仅“查找”),而且我一年只使用几次的工具要 37 美元。

其他建议?

【问题讨论】:

    标签: ms-access ms-access-2007 ms-access-2010


    【解决方案1】:

    我经常使用一些东西来找出某些函数或查询可能隐藏在意外的地方(例如,在子查询内的绑定控件的 RowSource 中)。

    我使用未记录的功能将所有 Access 对象导出为原始文本文件。
    使用可以在文件夹下递归搜索文件的文本编辑器(例如免费的Notepad++)然后我确信我可以找到特定字符串的所有出现,无论多么隐藏。

    导出所有对象的代码包括我的IsBlank() function

    '====================================================================
    ' Name:    DocDatabase
    ' Purpose: Documents the database to a series of text files
    ' From:    http://www.datastrat.com/Code/DocDatabase.txt
    ' Author:  Arvin Meyer
    ' Date:    June 02, 1999
    ' Comment: Uses the undocumented [Application.SaveAsText] syntax
    '          To reload use the syntax [Application.LoadFromText]
    '          Modified to set a reference to DAO 8/22/2005
    '          Modified by Renaud Bompuis to export Queries as proper SQL
    '====================================================================
    Public Sub DocDatabase(Optional path As Variant = Null)
        If IsBlank(path) Then
            path = Application.CurrentProject.path & "\" & Application.CurrentProject.Name & " - exploded view\"
        End If
    
        On Error Resume Next
        MkDir path 
        MkDir path & "\Forms\"
        MkDir path & "\Queries\"
        MkDir path & "\Queries(SQL)\"
        MkDir path & "\Reports\"
        MkDir path & "\Modules\"
        MkDir path & "\Scripts\"
    
        On Error GoTo Err_DocDatabase
        Dim dbs As DAO.Database
        Dim cnt As DAO.Container
        Dim doc As DAO.Document
        Dim i As Integer
    
        Set dbs = CurrentDb() ' use CurrentDb() to refresh Collections
    
        Set cnt = dbs.Containers("Forms")
        For Each doc In cnt.Documents
            Application.SaveAsText acForm, doc.Name, path & "\Forms\" & doc.Name & ".txt"
        Next doc
    
        Set cnt = dbs.Containers("Reports")
        For Each doc In cnt.Documents
            Application.SaveAsText acReport, doc.Name, path & "\Reports\" & doc.Name & ".txt"
        Next doc
    
        Set cnt = dbs.Containers("Scripts")
        For Each doc In cnt.Documents
            Application.SaveAsText acMacro, doc.Name, path & "\Scripts\" & doc.Name & ".txt"
        Next doc
    
        Set cnt = dbs.Containers("Modules")
        For Each doc In cnt.Documents
            Application.SaveAsText acModule, doc.Name, path & "\Modules\" & doc.Name & ".txt"
        Next doc
    
        Dim intfile As Long
        Dim filename as String
        For i = 0 To dbs.QueryDefs.count - 1
             Application.SaveAsText acQuery, dbs.QueryDefs(i).Name, path & "\Queries\" & dbs.QueryDefs(i).Name & ".txt"
             filename = path & "\Queries(SQL)\" & dbs.QueryDefs(i).Name & ".txt"
             intfile = FreeFile()
             Open filename For Output As #intfile
             Print #intfile, dbs.QueryDefs(i).sql
             Close #intfile
        Next i
    
        Set doc = Nothing
        Set cnt = Nothing
        Set dbs = Nothing
    
    Exit_DocDatabase:
        Debug.Print "Done."
        Exit Sub
    
    Err_DocDatabase:
        Select Case Err
    
        Case Else
            MsgBox Err.Description
            Resume Exit_DocDatabase
        End Select
    
    End Sub
    

    要使用它,只需从 Access IDE 的即时窗口中调用DocDatabase,它将在“Exploded View”文件夹下创建一组目录,其中包含所有文件。

    【讨论】:

    • +1 我添加了指向 IsBlank() 函数的链接。不错的博客。这对我很有用。
    • 我认为我更喜欢Optional path As String = "",但这可能只是我尽可能避免变体。
    • @HansUp:感谢您的补充,我经常使用IsBlank(),所以我经常忘记它不是语言的一部分。使用显式字符串而不是 Variant 是对的,因为性能很重要,我通常关心这些事情,而不是在这里通过我承认。
    • 一个小警告 - 如果对象名称包含在路径名称中无效的字符,则 MkDir 函数将崩溃。就我而言,名称中带有“*”的模块。
    • @Renaud:你成就了我的一天。我是 VBA 菜鸟,我一直在寻找可以立即在我的数据库上执行的东西。它就像一个魅力。将一些查询转换为 SQL 有点麻烦。 (级联连接的问题)但我不介意非 SQL 查询导出是可以的。您实际上已经解决了我对 Access 代码的版本控制的问题。非常感谢
    【解决方案2】:

    另一个选项是暂时打开 NAME AUTOCORRECT 选项。这是一个实施得很糟糕的功能,如果将其用于生产部署,可能会损坏您的数据库,但我经常在接管其他人创建的 Access 应用程序时使用它,以便将其转换为使用我的命名约定。

    您基本上打开它,让它构建依赖关系表,然后进行更改。然后,您可以遍历依赖关系树以确认它得到了所有依赖关系。完成后,将其关闭。

    但是,它不适用于 VBA 代码。但是对于更改字段名称等,如果仔细使用它是非常有用的。

    【讨论】:

    【解决方案3】:

    我修改了上面的代码,去掉了对象名中带有“~”的临时对象,如下:

    Set cnt = dbs.Containers("Scripts")
    For Each doc In cnt.Documents
        If Not doc.Name Like "~*" Then
            Application.SaveAsText acMacro, doc.Name, path & "\Scripts\" & doc.Name & ".txt"
        End If
    Next doc
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-24
      • 2021-03-05
      • 1970-01-01
      • 2021-05-26
      • 2013-03-09
      • 2013-10-17
      • 2013-08-22
      相关资源
      最近更新 更多