2011-10-20 14 views
13

Ho un database. In questo ho centinaia di tabelle, macro e moduli. Nessun problema, devo trovare tutte le query, le macro correlate a una tabella specifica.Come trovare tutte le query relative alla tabella in MS Access

sto usando Microsoft Acess 2000.

Ma ho anche ho provato dipendenze Objet di Access 2007, ha mostrato automaticamente un sacco di errori e stretta.

È questo un modo semplice per ottenere questo ???

Grazie, Shanmugam

risposta

22

Si può cercare di eseguire query SQL contro le tabelle di sistema direttamente per ottenere le dipendenze che appaiono 2003+ versioni in altro modo user-friendly. Non sono sicuro se funziona nel 2000 (lo fa in 2003+), ma vale la pena provare:

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

potrebbe essere necessario per verificare se si dispone delle autorizzazioni per le tabelle di sistema l'accesso ...

Speranza questo aiuta

+1

Ciao Igor Turman, è stato davvero utile. puoi dire come trovare query e tabelle relative a una query specifica? – shanmugamgsn

6

È possibile acquistare software di terze parti che faranno questo per voi, ma non ne ho mai sentito il bisogno. Invece, ho scritto un paio di procedure che lo faranno. Richiedono un riferimento a DAO.

Il primo (SearchQueries) ricerca solo il testo delle query e viene eseguito abbastanza velocemente. Il secondo (SearchDB) ricerca forme, macro, query, report e codice. Ci vuole un po 'di più ma è molto completo. L'uso dovrebbe essere abbastanza auto-esplicativo ma porre domande se non sei sicuro di nulla.

Ecco il testo completo delle procedure:

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 
+0

Grazie per la tua risposta mwolfe02. (Perdonami per aver risposto tardi). Come hai detto, il primo modulo era semplice e facile da capire. Mentre il secondo era troppo complesso per me. Ma non ho ancora provato entrambi. Vado in ufficio da Tomo e provo entrambi e ti faccio sapere. Se non capissi ulteriormente, lo farò sapere. – shanmugamgsn

+1

Grazie mille. Ricorda che per il codice da eseguire devi fare riferimento a DAO (Strumenti | Riferimenti | Aggiungi oggetti Microsoft Office Data Acceess Engine ...) – ankostis

+0

@ mwolfe02 Questa potrebbe essere una domanda stupida, ma perché hai due termini Err_Search DB quando il programma dovrebbe andare su Err_SearchDB per errore? – 114

2
SELECT DISTINCT 
MSysObjects.Name, MSysQueries.Name1, MSysQueries.Name2, MSysQueries.Expression 
FROM 
MSysQueries 
INNER JOIN 
MSysObjects ON MSysQueries.ObjectId = MSysObjects.Id; 

Questo mi ha dato un tavolo di tutto ciò che cercavo. Grazie Igor.

+1

Puoi [modificare] spiegare i cambiamenti che hai apportato alla risposta di Igor che ha portato a questo? –

+0

Questo è stato estremamente utile, grazie! –

2

Per gli altri che trovano questa pagina come ho fatto, di seguito è una variazione che include le occorrenze di una stringa, in tutte le tabelle di query o espressioni. (Funzionava sia in Access 2003 che in 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# " 
;