vba DoCmd.SetWarnings 和 CurrentDB.Execute 有什么区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11213892/
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 16:37:12  来源:igfitidea点击:

What's the difference between DoCmd.SetWarnings and CurrentDB.Execute

sqlvbams-accesserror-handlingaccess-vba

提问by JimmyPena

In the comments on this answer, Remou writes that

对此答案的评论中,Remou 写道

CurrentDB.Execute "insert sql here"

CurrentDB.Execute "insert sql here"

is better than

DoCmd.SetWarnings = 0
DoCmd.RunSQL "insert sql here"

DoCmd.SetWarnings = 0
DoCmd.RunSQL "insert sql here"

due to the built-in warnings that Access produces. I'm trying to understand the difference.

由于 Access 产生的内置警告。我试图理解其中的区别。

If they both mask errors, why is the first one preferable over the second? Are there any best practices here?

如果它们都掩盖了错误,为什么第一个比第二个更可取?这里有什么最佳实践吗?

回答by Fionnuala

They do not both mask errors. DoCmd.SetWarnings masks errors and is system wide, not confined to the single application that you are using. DoCmd.SetWarnings Falsewithout the corresponding DoCmd.SetWarnings Truewill mean that action queries will run without any prompts in any Access application on the PC.

它们不会同时掩盖错误。DoCmd.SetWarnings 屏蔽错误并且是系统范围的,不限于您正在使用的单个应用程序。DoCmd.SetWarnings False没有相应的 DoCmd.SetWarnings True将意味着操作查询将在 PC 上的任何 Access 应用程序中运行而没有任何提示。

Execute does throw warnings, the warnings that you need, such as the query failed to execute, but does not give warnings you may not need, such as "Are you sure you want to run this query".

Execute 确实会抛出警告,您需要的警告,例如查询无法执行,但不会给出您可能不需要的警告,例如“您确定要运行此查询吗”。

In this threadAllen Browne, Access MVP, says he does not use Set Warnings.

在这个帖子中,Access MVP Allen Browne 说他不使用设置警告。

As an aside, I would generally recommend using an instance of CurrentDB, as this will allow you to return a record count, amongst other things, so:

顺便说一句,我通常建议使用 CurrentDB 的实例,因为这将允许您返回记录计数等,因此:

Set db = CurrentDB
db.Execute sSQL, dbFailOnError