Public Type jobListType
Position As String
Level As Integer
ObjectName As String
End Type
Sub QueryDependencies()
On Error GoTo errortrap
Dim QueryName As String
Dim RecordIn As New ADODB.Recordset
Dim RecordOut As New ADODB.Recordset
Dim qdfTemp As QueryDef
Dim td As TableDef
Dim counter(100) As Integer
Dim objects(1000) As String
Dim jobList(1000) As jobListType
Dim sqlstr As String
Dim CurrentLevel As Integer
Dim posStr As String
Dim repeat As Boolean
Dim jobCt As Integer
Dim waitingCt As Integer
Dim n As Integer
QueryName = InputBox("Enter Query Name to trace dependencies.", "Query Dependency Trace")
'set up a table for results
DoCmd.DeleteObject acTable, "tblQueryDependencies"
Set td = CurrentDb().CreateTableDef("tblQueryDependencies")
td.Fields.Append td.CreateField("Position", dbText, 255)
td.Fields.Append td.CreateField("Level", dbInteger)
td.Fields.Append td.CreateField("ObjectName", dbText, 255)
CurrentDb().TableDefs.Append td
'process the query
RecordOut.Open "SELECT * FROM tblQueryDependencies;", CurrentProject.Connection, adOpenKeyset, adLockPessimistic
With RecordIn
Do
repeat = False
sqlstr = "SELECT o.Name, o1.Name AS [References], o1.Type AS TypeOf FROM MSysObjects AS o1 RIGHT JOIN (MSysQueries AS q INNER JOIN MSysObjects AS o ON q.ObjectId = o.Id) ON o1.Name = q.Name1 GROUP BY o.Name, o1.Name, o1.Type, o.Type, o.Flags HAVING (((o.Name)='" & QueryName & "') AND ((o1.Type) Is Not Null) AND ((o.Type)=5) AND ((o.Flags)<>3)) ORDER BY o1.Type;"
.Open sqlstr, CurrentProject.Connection, adOpenKeyset, adLockPessimistic
If .RecordCount > 0 Then
'write the parent record
If CurrentLevel = 0 Then
RecordOut.AddNew
RecordOut!Position = "10"
RecordOut!Level = 0
RecordOut!ObjectName = QueryName
RecordOut.Update
posStr = "10-"
End If
.MoveFirst
counter(CurrentLevel) = 10
'loop through the objects
Do While Not .EOF
'if it is a query, add to the jobList as we need to see what makes up each query
If !TypeOf = 5 Then
jobList(jobCt).Position = posStr & counter(CurrentLevel)
jobList(jobCt).Level = CurrentLevel + 1
jobList(jobCt).ObjectName = !References
jobCt = jobCt + 1
waitingCt = waitingCt + 1
End If
'write it to the table
RecordOut.AddNew
RecordOut!Position = posStr & counter(CurrentLevel)
RecordOut!Level = CurrentLevel + 1
RecordOut!ObjectName = !References
RecordOut.Update
counter(CurrentLevel) = counter(CurrentLevel) + 1
.MoveNext
Loop
.Close
End If
'find the next object to examine
If waitingCt > 0 Then
For n = 0 To jobCt - 1
If jobList(n).ObjectName <> "" Then
QueryName = jobList(n).ObjectName
jobList(n).ObjectName = ""
CurrentLevel = jobList(n).Level
posStr = jobList(n).Position & "-"
waitingCt = waitingCt - 1
repeat = True
Exit For
End If
Next n
End If
Loop While repeat = True
End With
RecordOut.Close
Set RecordIn = Nothing
Set RecordOut = Nothing
'open the output view
DoCmd.DeleteObject acQuery, "qry_XTab_QueryDependencies"
Set qdfTemp = CurrentDb().CreateQueryDef("qry_XTab_QueryDependencies", "TRANSFORM Min(tblQueryDependencies.ObjectName) AS MinOfObjectName SELECT tblQueryDependencies.Position FROM tblQueryDependencies GROUP BY tblQueryDependencies.Position ORDER BY tblQueryDependencies.Position PIVOT tblQueryDependencies.Level;")
DoCmd.OpenQuery "qry_XTab_QueryDependencies"
Exit Sub
errortrap:
Select Case Err
Case 7874
Resume Next
Case Else
MsgBox ("Error " & Err & " : " & Error(Err))
End Select
End Sub