2013-12-17 46 views

回答

10

對於鏈接表,TableDef.Connect屬性包含連接信息。但對於本地表,.Connect屬性是一個空字符串。

這樣你就可以知道哪些是其通過.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 

檢查Len()這種方法比較簡單,我比tdf.Attributes And dbAttachedODBC Or tdf.Attributes And dbAttachedTable,或ADOX方法記住。它也比ADOX方法簡潔得多。

3

基於http://p2p.wrox.com/access-vba/37117-finding-linked-tables.html(鏈接的代碼導致無限循環,所以我修改它):

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 

直接從http://p2p.wrox.com/access-vba/37117-finding-linked-tables.html,所述ADO對方(未驗證代碼)摘自:

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