如何在TableDefs中搜索链接表?

Sto*_*ckB 7 ms-access dao ado access-vba

通过一组TableDef循环,如何确定每个TableDef是否表示链接表,而不是本地表?

Han*_*sUp 12

对于链接表,该TableDef.Connect属性包含连接信息.但对于本机表,该.Connect属性是一个空字符串.

所以,你可以知道哪些是其通过检查Len().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
Run Code Online (Sandbox Code Playgroud)

这种方法比我tdf.Attributes And dbAttachedODBC Or tdf.Attributes And dbAttachedTable或ADOX方法更容易记住.它也比ADOX方法简洁得多.


Sto*_*ckB 6

基于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
Run Code Online (Sandbox Code Playgroud)

直接从ADO对应版本(未经验证的代码)http://p2p.wrox.com/access-vba/37117-finding-linked-tables.html中获取

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
Run Code Online (Sandbox Code Playgroud)