如何在MS Access中查找与表相关的所有查询

时间:2011-10-20 04:13:07

标签: ms-access ms-access-2007

我有一个数据库。在这里我有数百个表,宏和表格。 不,我的问题是我必须找到与特定表相关的所有查询,宏。

我正在使用microsoft acess 2000。

但我甚至在Access 2007中尝试了objet依赖项,它显示了大量错误并自动关闭。

这有什么简单的方法来获得这个???

谢谢, 尚穆根

4 个答案:

答案 0 :(得分:25)

您可以尝试直接对系统表执行SQL查询,以更加用户友好的方式获取2003+版本中显示的依赖项。我不确定它是否适用于2000(它在2003年以上),但值得尝试:

SELECT DISTINCT MSysObjects.Name
FROM MSysQueries INNER JOIN MSysObjects ON MSysQueries.ObjectId=MSysObjects.Id
WHERE (((MSysQueries.Name1) Like "*" & [TableName] & "*")) OR (((MSysQueries.Name2) Like "*" & [TableName] & "*"))

您可能需要检查您是否有权访问系统表...

希望这有帮助

答案 1 :(得分:7)

你可以购买第三方软件来为你做这件事,但我从来没有觉得有必要这样做。相反,我写了几个会这样做的程序。他们需要参考DAO。

第一个(SearchQueries)仅搜索查询文本并且运行得非常快。第二个(SearchDB)搜索表单,宏,查询,报告和代码。它需要更长的时间但是非常彻底。用法应该是不言自明的,但如果你不确定,可以提出问题。

以下是程序的全文:

Sub SearchQueries(SearchText As String, _
                  Optional ShowSQL As Boolean = False, _
                  Optional QryName As String = "*")
    On Error Resume Next
    Dim QDef As QueryDef

    For Each QDef In CurrentDb.QueryDefs
        If QDef.Name Like QryName Then
            If InStr(QDef.SQL, SearchText) > 0 Then
                Debug.Print QDef.Name
                If ShowSQL Then Debug.Print QDef.SQL & vbCrLf
            End If
        End If
    Next QDef
End Sub


'Updated: 1/19/09 Limit search by object name pattern
Sub SearchDB(SearchText As String, _
             Optional ObjType As AcObjectType = acDefault, _
             Optional ObjName As String = "*")
Dim db As Database, obj As AccessObject, Ctl As Control, Prop As Property
Dim Frm As Form, Rpt As Report, mdl As Module
Dim objLoaded As Boolean, Found As Boolean, Instances As Long
Dim SLine As Long, SCol As Long, ELine As Long, ECol As Long

    On Error GoTo Err_SearchDB

    Set db = CurrentDb
    Application.Echo False

    '===============================================
    'Search queries
    If ObjType = acDefault Or ObjType = acQuery Then
        Debug.Print "Queries:"
        SearchQueries SearchText, False, ObjName
        Debug.Print vbCrLf
    End If


    '===============================================
    'Search forms
    If ObjType = acDefault Or ObjType = acForm Then
        Debug.Print "Forms:"
        On Error Resume Next
        For Each obj In CurrentProject.AllForms
            If obj.Name Like ObjName Then
                objLoaded = obj.IsLoaded
                If Not obj.IsLoaded Then DoCmd.OpenForm obj.Name, acDesign, , , , acHidden
                Set Frm = Application.Forms(obj.Name)
                For Each Prop In Frm.Properties
                    Err.Clear
                    If InStr(Prop.Value, SearchText) > 0 Then
                        If Err.Number = 0 Then
                            Debug.Print "Form: " & Frm.Name & _
                                        "  Property: " & Prop.Name & _
                                        "  Value: " & Prop.Value
                        End If
                    End If
                Next Prop
                If Frm.HasModule Then
                    SLine = 0: SCol = 0: ELine = 0: ECol = 0: Instances = 0
                    Found = Frm.Module.Find(SearchText, SLine, SCol, ELine, ECol)
                    Do Until Not Found
                        Instances = Instances + 1
                        SLine = ELine + 1: SCol = 0: ELine = 0: ECol = 0
                        Found = Frm.Module.Find(SearchText, SLine, SCol, ELine, ECol)
                    Loop
                    If Instances > 0 Then Debug.Print "Form: " & Frm.Name & _
                       "  Module: " & Instances & " instances"

                End If
                For Each Ctl In Frm.Controls
                    For Each Prop In Ctl.Properties
                        Err.Clear
                        If InStr(Prop.Value, SearchText) > 0 Then
                            If Err.Number = 0 Then
                                Debug.Print "Form: " & Frm.Name & _
                                            "  Control: " & Ctl.Name & _
                                            "  Property: " & Prop.Name & _
                                            "  Value: " & Prop.Value
                            End If
                        End If
                    Next Prop
                Next Ctl
                Set Frm = Nothing
                If Not objLoaded Then DoCmd.Close acForm, obj.Name, acSaveNo
                DoEvents
            End If
        Next obj
        On Error GoTo Err_SearchDB
        Debug.Print vbCrLf
    End If


    '===============================================
    'Search modules
    If ObjType = acDefault Or ObjType = acModule Then
        Debug.Print "Modules:"
        For Each obj In CurrentProject.AllModules
            If obj.Name Like ObjName Then
                objLoaded = obj.IsLoaded
                If Not objLoaded Then DoCmd.OpenModule obj.Name
                Set mdl = Application.Modules(obj.Name)
                SLine = 0: SCol = 0: ELine = 0: ECol = 0: Instances = 0
                Found = mdl.Find(SearchText, SLine, SCol, ELine, ECol)
                Do Until Not Found
                    Instances = Instances + 1
                    SLine = ELine + 1: SCol = 0: ELine = 0: ECol = 0
                    Found = mdl.Find(SearchText, SLine, SCol, ELine, ECol)
                Loop
                If Instances > 0 Then Debug.Print obj.Name & ": " & Instances & " instances"
                Set mdl = Nothing
                If Not objLoaded Then DoCmd.Close acModule, obj.Name
            End If
        Next obj
        Debug.Print vbCrLf
    End If


    '===============================================
    'Search macros
    If ObjType = acDefault Or ObjType = acMacro Then
        'Debug.Print "Macros:"
        'Debug.Print vbCrLf
    End If


    '===============================================
    'Search reports
    If ObjType = acDefault Or ObjType = acReport Then
        Debug.Print "Reports:"
        On Error Resume Next
        For Each obj In CurrentProject.AllReports
            If obj.Name Like ObjName Then
                objLoaded = obj.IsLoaded
                If Not obj.IsLoaded Then DoCmd.OpenReport obj.Name, acDesign
                Set Rpt = Application.Reports(obj.Name)
                For Each Prop In Rpt.Properties
                    Err.Clear
                    If InStr(Prop.Value, SearchText) > 0 Then
                        If Err.Number = 0 Then
                            Debug.Print "Report: " & Rpt.Name & _
                                        "  Property: " & Prop.Name & _
                                        "  Value: " & Prop.Value
                        End If
                    End If
                Next Prop
                If Rpt.HasModule Then
                    SLine = 0: SCol = 0: ELine = 0: ECol = 0: Instances = 0
                    Found = Rpt.Module.Find(SearchText, SLine, SCol, ELine, ECol)
                    Do Until Not Found
                        Instances = Instances + 1
                        SLine = ELine + 1: SCol = 0: ELine = 0: ECol = 0
                        Found = Rpt.Module.Find(SearchText, SLine, SCol, ELine, ECol)
                    Loop
                    If Instances > 0 Then Debug.Print "Report: " & Rpt.Name & _
                       "  Module: " & Instances & " instances"

                End If
                For Each Ctl In Rpt.Controls
                    For Each Prop In Ctl.Properties
                        If InStr(Prop.Value, SearchText) > 0 Then
                            Debug.Print "Report: " & Rpt.Name & _
                                        "  Control: " & Ctl.Name & _
                                        "  Property: " & Prop.Name & _
                                        "  Value: " & Prop.Value
                        End If
                    Next Prop
                Next Ctl
                Set Rpt = Nothing
                If Not objLoaded Then DoCmd.Close acReport, obj.Name, acSaveNo
                DoEvents
            End If
        Next obj
        On Error GoTo Err_SearchDB
        Debug.Print vbCrLf
    End If

Exit_SearchDB:
    Application.Echo True
    Exit Sub
Err_SearchDB:
    Application.Echo True
    Debug.Print Err.Description
    Debug.Assert False
    Resume
End Sub

答案 2 :(得分:2)

build.gradle

这给了我一张我正在寻找的东西的表格。谢谢伊戈尔。

答案 3 :(得分:2)

对于像我一样找到此页面的其他人,下面是一个变体,包括在所有查询的表或表达式中出现的字符串。 (这在Access 2003和Access 2013中均有效。)

SELECT DISTINCT 
MSysObjects.Name, MSysQueries.Name1, MSysQueries.Name2, MSysQueries.Expression
FROM 
MSysQueries 
INNER JOIN 
MSysObjects ON MSysQueries.ObjectId = MSysObjects.Id
WHERE 
(   (((MSysQueries.Name1) Like "*" & [String to search for] & "*")) 
 OR (((MSysQueries.Name2) Like "*" & [String to search for] & "*"))
 OR (((MSysQueries.Expression) Like "*" & [String to search for] & "*"))  )

And "Comment:  You will be prompted once, for the [String to search for]"<>""
And "Comment:  The starting point for this code came from link:"<>
"http://stackoverflow.com/questions/7831071/how-to-find-all-queries-related-to-table-in-ms-access# "
;