Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top