文本搜索属性访问对象

时间:2011-06-20 09:06:35

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

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

我问这个是因为如果我改变一个表中字段的名称,我要检查很多对象属性(Record Source,Control Source,Order By,...)。这可以通过跟踪错误或通过检查表单的每个控件的所有属性来完成,但这需要花费很多时间。

一个选项是Find and Replace工具(漂亮的工具!),但这对我来说有点过分。我不需要文本替换(只有'查找'),一个工具只需37美元,我一年只会使用几次。

其他建议?

3 个答案:

答案 0 :(得分:14)

我经常使用一些东西来找出某些函数或查询可能隐藏在某处意外的位置(例如在子查询中的绑定控件的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,它将在“爆炸视图”文件夹下创建一组目录,其中包含所有文件。

答案 1 :(得分:1)

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

你基本上打开它,让它构建依赖关系表,然后进行更改。然后,您可以遍历依赖关系树以确认它已全部完成。完成后,将其关闭。

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

答案 2 :(得分:1)

我修改了上面的代码,在对象名称中删除带有“〜”的临时对象,如下所示:

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