Remarks
This method will return a run-time error if any of the following conditions are true:
Access does not search Visual Basic for Applications (VBA) code, macros, or data access pages for dependencies.
- The Track name AutoCorrect info setting (Tools menu > Options dialog box > General tab) is disabled. Use the following code to enable the Track name AutoCorrect info setting and update the dependency information for all of the objects in the database: Application.SetOption "Track Name AutoCorrect Info", 1
- You have insufficient permissions to check the dependency information for the specified AccessObject object.
- This method is being called from an Access project (.adp).
I'm not getting a runtime error, it's a compile error.How are you calling it? Can you post a screenshot of the error you get?
Have you checked the help file?
Dim tdf As DAO.Tabledef
Dim rs As DAO.Recordset
I have altered the code toWhat happens if you change your declarations to:
Code:Dim tdf As DAO.Tabledef Dim rs As DAO.Recordset
Private Sub getDependency()
Dim sSql As String
Dim tdf As dao.TableDef
Dim rs As dao.Recordset
Dim db As Database
Set db = CurrentDb
DoCmd.RunSQL "DELETE * FROM Depend_Data"
Set rs = db.OpenRecordset("Depend_Data", dbOpenDynaset)
For Each tdf In db.TableDefs
If InStr(tdf.Name, "~") = 0 Then
rs.AddNew
rs!TableName = tdf.Name
rs!Depends = tdf.GetDependencyInfo
rs.Update
End If
Next tdf
End Sub
Public Function oDB() As dao.Database
If pCurrentDb Is Nothing Then
Set pCurrentDb = CurrentDb
End If
Set oDB = pCurrentDb
End Function
dao
does not automatically update to DAO
suggests something might be off, like a missing reference or a mis-declared variable.Option Explicit
declared at the top of every code module?Yes, Compare datbase and Option Explicit is at the top of each and every modiule,The fact thatdao
does not automatically update toDAO
suggests something might be off, like a missing reference or a mis-declared variable.
Do you haveOption Explicit
declared at the top of every code module?
Does your code compile?
Does your code compile if you comment out yourand no, the code does not compile.
getDependency()
sub?Yes, I have used that code to list table names and fields as well as listing queries and their sql.Does your code compile if you comment out yourgetDependency()
sub?
Option Compare Database
Option Explicit
Private Sub btnstart_Click()
DoCmd.SetWarnings False
GetTableData
getQueryData
' getDependency
DoCmd.SetWarnings True
Me.btnStart.Caption = "Done"
Pause 3
DoCmd.Close acForm, Me.Name
End Sub
Private Sub GetTableData()
Dim sSql As String
Dim tdf As TableDef
Dim x As Integer
Dim rs As Recordset
DoCmd.RunSQL "DELETE * FROM Table_Data"
Set rs = oDB.OpenRecordset("Table_Data", dbOpenDynaset)
For Each tdf In oDB.TableDefs
If InStr(tdf.Name, "~") = 0 Then ' Don't enumerate the system tables
For x = 0 To tdf.Fields.Count - 1
If InStr(tdf.Fields(x).Name, "main") > 0 Or InStr(tdf.Fields(x).Name, " ") > 0 Then
rs.AddNew
rs!TableName = tdf.Name
rs!FieldName = tdf.Fields(x).Name
rs.Update
End If
Next x
End If
Next tdf
End Sub
Private Sub getQueryData()
Dim i As Integer
Dim rs As Recordset
DoCmd.RunSQL "DELETE * FROM Query_Data"
Set rs = oDB.OpenRecordset("Query_Data", dbOpenDynaset)
On Error Resume Next
For i = 0 To oDB.QueryDefs.Count - 1
If InStr(oDB.QueryDefs(i).Name, "~") = 0 Then
rs.AddNew
rs!query_name = oDB.QueryDefs(i).Name
rs!query_text = oDB.QueryDefs(i).SQL
rs.Update
End If
Next
End Sub
I've just done a compact and repair and the doa stays in lower case.Yes, Compare datbase and Option Explicit is at the top of each and every modiule,
and no, the code does not compile.
It does create an object (TableDef object), it just doesn't create an AccessObject object.it would seem that "tdf as tabledef" does not create an object.
Application.CurrentData.AllTables.Item("TableName")
AllTables
collection)Perhaps you have a local variable in your code somewhere namedI've just done a compact and repair and the doa stays in lower case.
dao
?That compiles but I now get run time error 438.It does create an object (TableDef object), it just doesn't create an AccessObject object.
You have to access your tables viaApplication.CurrentData.AllTables.Item("TableName")
(or loop theAllTables
collection)
Private Sub getDependency()
Dim sStr As String
Dim tdf As TableDef
Dim rs As Recordset
' Dim db As Database
' Set db = CurrentDb
DoCmd.RunSQL "DELETE * FROM Depend_Data"
Set rs = oDB.OpenRecordset("Depend_Data", dbOpenDynaset)
For Each tdf In oDB.TableDefs
If InStr(tdf.Name, "~") = 0 Then
sStr = tdf.Name
rs.AddNew
rs!TableName = tdf.Name
rs.Update
rs!Depends = Application.CurrentData.AllTables.item(sStr).GetDependencyInfo.dependency
' rs!Depends = Application.CurrentData.AllTables.item(tdf.Name).GetDependencyInfo.dependency
rs.Update
End If
Next tdf
End Sub
The most effective way to answer that question might be to go to the back end and try it. Then you could report what actually happens.That compiles but I now get run time error 438.
I have tried with a string variable holding the table name and tdf.name. See the code.
It loops past the system and temp "~" tables then seems to be searching and eventually crashes on the first table.
Question, will this work on linked tables or do I need to go to the back end?
Code:Private Sub getDependency() Dim sStr As String Dim tdf As TableDef Dim rs As Recordset ' Dim db As Database ' Set db = CurrentDb DoCmd.RunSQL "DELETE * FROM Depend_Data" Set rs = oDB.OpenRecordset("Depend_Data", dbOpenDynaset) For Each tdf In oDB.TableDefs If InStr(tdf.Name, "~") = 0 Then sStr = tdf.Name rs.AddNew rs!TableName = tdf.Name rs.Update rs!Depends = Application.CurrentData.AllTables.item(sStr).GetDependencyInfo.dependency ' rs!Depends = Application.CurrentData.AllTables.item(tdf.Name).GetDependencyInfo.dependency rs.Update End If Next tdf End Sub
I'm half way there, doing a decompile recompile at the moment but will certainly post the results as I find themThe most effective way to answer that question might be to go to the back end and try it. Then you could report what actually happens.
That compiles but I now get run time error 438.
I have tried with a string variable holding the table name and tdf.name. See the code.
It loops past the system and temp "~" tables then seems to be searching and eventually crashes on the first table.
Question, will this work on linked tables or do I need to go to the back end?
Code:Private Sub getDependency() Dim sStr As String Dim tdf As TableDef Dim rs As Recordset ' Dim db As Database ' Set db = CurrentDb DoCmd.RunSQL "DELETE * FROM Depend_Data" Set rs = oDB.OpenRecordset("Depend_Data", dbOpenDynaset) For Each tdf In oDB.TableDefs If InStr(tdf.Name, "~") = 0 Then sStr = tdf.Name rs.AddNew rs!TableName = tdf.Name rs.Update rs!Depends = Application.CurrentData.AllTables.item(sStr).GetDependencyInfo.dependency ' rs!Depends = Application.CurrentData.AllTables.item(tdf.Name).GetDependencyInfo.dependency rs.Update End If Next tdf End Sub
Private Sub getDependency()
Dim sStr As String
Dim tbl As Access.AccessObject
Dim rs As DAO.Recordset
' Dim db As Database
' Set db = CurrentDb
DoCmd.RunSQL "DELETE * FROM Depend_Data"
Set rs = oDB.OpenRecordset("Depend_Data", dbOpenDynaset)
For Each tbl In Application.CurrentData.AllTables
If InStr(tbl.Name, "~") = 0 Then
sStr = tbl.Name
rs.AddNew
rs!TableName = sStr
rs!Depends = tbl.GetDependencyInfo.dependency ' <-- I don't know whether this is correct
rs.Update
End If
Next tdf
rs.Close
Set rs = Nothing
End Sub
Two things happened.The most effective way to answer that question might be to go to the back end and try it. Then you could report what actually happens.
rs!Depends = tbl.GetDependencyInfo.dependency gives method not recognised as a compile errorI'd imagine you'd want something more like:
Code:Private Sub getDependency() Dim sStr As String Dim tbl As Access.AccessObject Dim rs As DAO.Recordset ' Dim db As Database ' Set db = CurrentDb DoCmd.RunSQL "DELETE * FROM Depend_Data" Set rs = oDB.OpenRecordset("Depend_Data", dbOpenDynaset) For Each tbl In Application.CurrentData.AllTables If InStr(tbl.Name, "~") = 0 Then sStr = tbl.Name rs.AddNew rs!TableName = sStr rs!Depends = tbl.GetDependencyInfo.dependency ' <-- I don't know whether this is correct rs.Update End If Next tdf rs.Close Set rs = Nothing End Sub