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?

More than 1 year has passed since last update.

Accessでクエリを表示したままALTER TABLEすると例外になる

Last updated at Posted at 2023-06-07

例外が発生する条件

下記のようなテーブル。
image.png

下記のようなクエリ。
image.png

クエリを表示した状態でボタン押す。
image.png

ボタン押した時の処理
Private Sub btn_1_Click()

    Dim com_ As New ADODB.Command: com_.ActiveConnection = CurrentProject.Connection
    
    com_.CommandText = "DELETE FROM tes_1"
    com_.Execute
    
    com_.CommandText = "ALTER TABLE tes_1 ALTER COLUMN record_id COUNTER (1,1)"
    com_.Execute 'ここで例外。

End Sub

例外時のダイアログは下記。
image.png

テーブル※※は現在ほかのユーザーまたはプロセスで使用されているので、ロックできませんでした。

蛇足

ALTER TABLEやる前にクエリを閉じる処理をかませよう。

クエリを閉じる
DoCmd.Close acQuery, "クエリ名", acSaveNo

『もしくはクエリが一つでも開いていたら実行不可』といった制御。

参考サイトさん

バージョン

Windows 10 Pro 22H2 19045.3031
Microsoft Access for Microsoft 365 MSO (バージョン 2304 ビルド 16.0.16327.20200) 32 ビット

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?