glgcag1991
Programmer
I am writing code to delete links to my SQL Server tables and when I run the function, it doesn't delete all the tables. I have a total of 89 linked SQL tables and I have to run the function 5 times to delete all the tables. Here's the code I'm running:
Any help is appreciated!
Code:
Function DeleteSQLTableLinks()
'On Error Resume Next
Dim cn As New ADODB.Connection
Dim cat As New ADOX.Catalog
Dim tbl As New ADOX.Table
Dim lCnt As Long
'Types of tables: TABLE=Access mdb table; ACCESS TABLE=Access mdb system table
'VIEW=Access mdb query; PASS-THROUGH=SQL Server linked table
cn.ConnectionString = CurrentProject.Connection
cn.Open
cat.ActiveConnection = cn
lCnt = 0
For Each tbl In cat.Tables
If tbl.Type = "PASS-THROUGH" Then
lCnt = lCnt + 1
Debug.Print lCnt & ". " & tbl.Name & " --- " & tbl.Type
cat.Tables.Delete (tbl.Name)
cat.Tables.Refresh
End If
Next tbl
cn.Close
Set cn = Nothing
Set cat = Nothing
End Function
Any help is appreciated!