iT邦幫忙

0

Access 2016 VBA 關閉所有 Table, Query, Report, Form

各位大大好,

我想要在 vba 可以寫個關閉所有開啟在 Access 2016 裡面開啟的東西.
我 google 了一下 找到了下面的 coding, 但是執行的時候跳出錯誤訊息 "編輯錯誤, 使用者自訂型態尚未定義" 不知道哪裡需要定義 怎麼定義? 麻煩大大幫一下忙, 謝謝.
或者有其他方法可以關閉所有視窗.

Private Sub Command0_Click()
Dim tbl As TableDef
Dim qr As QueryDef
Dim rpt As Report
Dim frm As Form
For Each tbl In CurrentDb.TableDefs
DoCmd.Close acTable, tbl.Name
Next tbl
For Each qr In CurrentDb.QueryDefs
DoCmd.Close acQuery, qr.Name
Next qr
For Each rpt In Application.Reports
DoCmd.Close acReport, rpt.Name
Next rpt
For Each frm In Application.Forms
If frm.Name <> "LoginFormName" Then DoCmd.Close acForm, frm.Name
Next frm
End Sub

matureox iT邦新手 5 級 ‧ 2021-05-19 09:03:00 檢舉
錯誤訊息的產生應該是找不到 TableDef and QueryDef 的自定義. (Report and Form 都可以關閉)
請問有人知道怎麼自定義 TableDef and QueryDef 嗎?
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
davidliu9116
iT邦研究生 2 級 ‧ 2022-04-08 10:37:50
最佳解答

Dim AccD As AccessObject, dbs As Object
Set dbs = Application.CurrentProject
For Each AccD In dbs.AllForms
If AccD.IsLoaded = True Then
DoCmd.Close acForm, AccD.Name, acSaveYes
End If
Next AccD
其他的自己發揮一下

matureox iT邦新手 5 級 ‧ 2022-04-22 13:34:27 檢舉

改成 AllQueries and acQuery 停在錯誤 dbs.AllQueries 那行
Dim AccD As AccessObject, dbs As Object
Set dbs = Application.CurrentProject
For Each AccD In dbs.AllQueries
If AccD.IsLoaded = True Then
DoCmd.Close acQuery, AccD.Name, acSaveYes
End If
Next AccD

davidliu9116 iT邦研究生 2 級 ‧ 2023-09-05 11:28:38 檢舉
抱歉,沒注意到,改一下寫法就可以了
   Dim AccD As AccessObject, dbs As Object
   Set dbs = Application.CurrentData.AllQueries
   For Each AccD In dbs
      If AccD.IsLoaded = True Then
         DoCmd.Close acQuery, AccD.name, acSaveYes
      End If
   Next AccD

我要發表回答

立即登入回答