14
votes

In the comments on this answer, Remou writes that

CurrentDB.Execute "insert sql here"

is better than

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

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

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

1

1 Answers

19
votes

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 False without the corresponding DoCmd.SetWarnings True will mean that action queries will run without any prompts in any Access application on the PC.

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".

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

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:

Set db = CurrentDB
db.Execute sSQL, dbFailOnError