やり方
Dim def_ As DAO.TableDef
For Each def_ In CurrentDb.TableDefs
'参考値としてテーブル名とAttributes値を出力。
Debug.Print def_.Name
Debug.Print def_.Attributes
If (def_.Attributes And TableDefAttributeEnum.dbSystemObject) Then
Debug.Print "システムテーブル"
End If
'ローカルテーブルはTableDefAttributeEnumに該当する値が無いため、
'Attributesの値を=で判定。
If def_.Attributes = 0 Then
Debug.Print "ローカルテーブル"
End If
If (def_.Attributes And TableDefAttributeEnum.dbAttachedODBC) Then
Debug.Print "ODBC接続のリンクテーブル"
End If
If (def_.Attributes And TableDefAttributeEnum.dbAttachedTable) Then
Debug.Print "ODBC接続ではないリンクテーブル"
End If
Debug.Print vbCrLf '出力が見やすいように改行挟む。
Next
なぜAndで判定?
テーブル種類の判定について、理解するのに少し時間かかったので自分なりに整理。
=で判定できない
TableDefAttributeEnum.dbAttachedODBC
は 『536870912』 です。
ですが、前述のサンプルだと、ODBC接続リンクテーブルのAttributesは 『537001984』 となっています。
536870912 ≠ 537001984
なので、
If def_.Attributes = TableDefAttributeEnum.dbAttachedODBC Then
のように =
で判定はできません。
じゃあAttributesに入っている値は何を表しているのか
Attributesの値『537001984』は『536870912 + 131072』と分割できます。
値 | 名前 | 説明 |
---|---|---|
536870912 | TableDefAttributeEnum.dbAttachedODBC | リンクされた ODBC データベース テーブル。 |
131072 | TableDefAttributeEnum.dbAttachSavePWD | リンクされたリモート テーブルのユーザー ID とパスワードを保存します。 |
Attributesには『ODBCリンクテーブル』かつ『IDとパスワードを保存している』という2つの情報が入っています。
なのでAndで判定が必要。
参考サイトさん
バージョン
Microsoft Windows [Version 10.0.19045.3324]
Microsoft Access for Microsoft 365 MSO (バージョン 2307 ビルド 16.0.16626.20170) 32 ビット