Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can one search TableDefs for linked tables?

Looping through a set of TableDefs, how can one determine if each TableDef represents a linked table, as opposed to a local table?

like image 886
StockB Avatar asked Dec 17 '13 19:12

StockB


2 Answers

For a linked table, the TableDef.Connect property contains the connection information. But for a native table, the .Connect property is an empty string.

So you can tell which are which by examining Len() of .Connect

Dim tdf As DAO.TableDef
With CurrentDb
    For Each tdf In .TableDefs
        If Len(tdf.Connect) > 0 Then
            Debug.Print tdf.Name, tdf.Connect
        End If
    Next
End With

That approach is simpler for me to remember than tdf.Attributes And dbAttachedODBC Or tdf.Attributes And dbAttachedTable, or the ADOX approach. It is also much more concise than the ADOX approach.

like image 158
HansUp Avatar answered Oct 06 '22 17:10

HansUp


Based on http://p2p.wrox.com/access-vba/37117-finding-linked-tables.html (the linked code resulted in an infinite loop, so I modified it):

Dim tdf As DAO.TableDef
With CurrentDb
    For Each tdf In .TableDefs
        If tdf.Attributes And dbAttachedODBC Or tdf.Attributes And dbAttachedTable Then
            ' We found a linked table! Now, show its location.
            Debug.Print tdf.Connect
        End If
    Next
End With

Taken directly from http://p2p.wrox.com/access-vba/37117-finding-linked-tables.html, the ADO counterpart (unverified code):

Sub ListAllTables(cnn As ADODB.Connection)

   Dim cat As ADOX.Catalog
   Dim tbl As ADOX.Table

   Set cat = New ADOX.Catalog
   cat.ActiveConnection = cnn

   cat.Tables.Refresh
   For Each tbl In cat.Tables
      Select Case tbl.Type
         Case "ACCESS TABLE"
            Debug.Print tbl.Name & " - Access table"
         Case "SYSTEM TABLE"
            Debug.Print tbl.Name & " - System table"
         Case "TABLE"
            Debug.Print tbl.Name & " - Native table"
         Case "LINK"
            Debug.Print tbl.Name & " - Linked table"
         Case "PASS-THROUGH"
            Debug.Print tbl.Name & " - ODBC DSN Linked table"
        End Select
    Next tbl

    Set tbl = Nothing
    Set cat = Nothing

End Sub
like image 37
StockB Avatar answered Oct 06 '22 17:10

StockB