访问:如何使用 VBA 检测查询是否已打开?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3992232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 12:13:59  来源:igfitidea点击:

access: how to detect with VBA whether a query is opened?

sqlvbams-accessaccess-vba

提问by l--''''''---------''''''''''''

is it possible to detect whether there is an open query using VBA in access-2007?

是否可以在 access-2007 中使用 VBA 检测是否有打开的查询?

i am opening a query like this:

我正在打开这样的查询:

    stDocName = "Meeting_Reasons_Frequency"
  DoCmd.OpenQuery stDocName

is it possible to detect whether it is open?

是否可以检测它是否打开?

回答by Fionnuala

How about:

怎么样:

 If SysCmd(acSysCmdGetObjectState, acQuery, "QueryName") = acObjStateOpen Then

More info: http://msdn.microsoft.com/en-us/library/aa205281(office.10).aspx

更多信息:http: //msdn.microsoft.com/en-us/library/aa205281(office.10).aspx

回答by pianoJames

Not sure if this applies to queries, but I noticed that for forms, you should use Andinstead of =.

不知道这是否适用于查询,但我注意到,对于表格,你应该使用And来代替=

If SysCmd(acSysCmdGetObjectState, acForm, "FormName") And acObjStateOpen Then

Apparently the form can have multiple "states" simultaneously. Using Andpicks out the one you want; it acts as a bitwise operator in this context.

显然,表单可以同时具有多个“状态”。使用And挑选出你想要的;在这种情况下,它充当按位运算符。

回答by HansUp

SysCmd(acSysCmdGetObjectState, acQuery, "QueryName")returns zero if QueryNameis closed or does not exist.

SysCmd(acSysCmdGetObjectState, acQuery, "QueryName")如果QueryName已关闭或不存在,则返回零。

Otherwise it returns the sum of the constants for whichever of the following apply:

否则,它返回适用于以下任一条件的常量总和:

  • acObjStateOpen(1) open in any state (new, dirty, etc.) or view (Design, Datasheet, etc.)
  • acObjStateDirty(2) open but with unsaved design changes
  • acObjStateNew(4) a new query which has not yet been saved to its parent collection (QueryDefs)
  • acObjStateOpen(1) 在任何状态(新的、脏的等)或视图(设计、数据表等)下打开
  • acObjStateDirty(2) 打开但有未保存的设计更改
  • acObjStateNew(4) 尚未保存到其父集合的新查询 ( QueryDefs)

Perhaps most of the time checking whether the SysCmdexpression = acObjStateOpenwill be sufficient. However if you want to avoid a surprise when the query is open with unsaved design changes you could And acObjStateOpenas pianoJames suggested.

也许大多数时候检查SysCmd表达式是否= acObjStateOpen足够。但是,如果您想避免在查询以未保存的设计更改打开时出现意外,您可以And acObjStateOpen按照PianoJames 的建议

But if your intention is to do something when QueryNameis open, you could simply check whether it is not closed (zero):

但是,如果您打算在QueryName打开时执行某些操作,您可以简单地检查它是否未关闭(零):

If SysCmd(acSysCmdGetObjectState, acQuery, "QueryName") <> 0 Then