0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ACCESS VBAでテーブルが既に存在する場合削除したい

Last updated at Posted at 2024-08-21

何気によくやる処理なのでメモです。

Sub DeleteTableIfExists(tblName As String)
    Dim dbs As DAO.Database
    Dim tdf As DAO.TableDef

    ' データベースの参照を取得
    Set dbs = CurrentDb()

    ' テーブルが存在するかどうかを確認
    On Error Resume Next
    Set tdf = dbs.TableDefs(tblName)
    If Err.Number = 0 Then
        ' テーブルが存在する場合、削除
        dbs.TableDefs.Delete tblName
        MsgBox "テーブル '" & tblName & "' が削除されました。"
    Else
        ' テーブルが存在しない場合
        MsgBox "テーブル '" & tblName & "' は存在しません。"
    End If
    On Error GoTo 0

    ' オブジェクトの解放
    Set tdf = Nothing
    Set dbs = Nothing
End Sub
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?