テーブルが存在するかどうか調べるには


Accessのデータベース上にテーブルが存在するかどうか調べるための関数 TableExists_FS() を紹介します。


構文

TableExists_FS(strTable As String) As Boolean

引数

指定項目 内容
strTable テーブル名を指定します

戻り値

Boolean 結果をTrue/Falseで返します。
True- テーブルが存在する
False-テーブルが存在しない 


TableExists_FS()

Function TableExists_FS(strTable As String) As Boolean
' For Access 97/2000
  Dim tdf As DAO.TableDef

  TableExists_FS = False
  For Each tdf In CurrentDb.TableDefs
      If strTable = tdf.Name Then
          TableExists_FS = True
          Exit For
      End If
  Next
End Function       


使用例

? TableExists_FS("MyTable")
True  

Tip:
Access 2000を使用する場合、CurrentDataオブジェクトのテーブルコレクションを使用する方法もあります。

Function TableExists2_FS(strTable As String) As Boolean
' For Access 2000
  Dim obj As AccessObject

  TableExists2_FS = False
  For Each obj In CurrentData.AllTables
      If strTable = obj.Name Then
          TableExists2_FS = True
          Exit For
      End If
  Next  
End Function