Proper User of Turning DoCmd.SetWarnings

AlexB123

Board Regular
Joined
Dec 19, 2014
Messages
207
Hi all,

Just a question, no real specific issue (at the moment). What are the best practices for turning DoCmd.SetWarnings to True vs. False?

I've seen some programmers set it to false, run a couple lines of code, then set it back to true, and do that repeatedly throughout a program.

My preference is to set it to false at the beginning of a program, and turn it back on at the end.

Is there an absolute right answer? Or is there a reason to switch between the two? Any guides for future use are greatly appreciated!!!
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
My preference is to set it to false at the beginning of a program, and turn it back on at the end.
Doing that could suppress unexpected error messages that you need to know about (and lead to unexpected or erroneous results without any warning or idea why)!

Basically, you want to identify a specific situation in which you want to suppress a warning, and suppress that warning only. Which is why you see it set to False, run the line or two of code, and then set it to True again.
 
Upvote 0
But what if you turn all the messages on during testing, and then remove them from all but the beginning and end of a program? Does that not address your point?
 
Upvote 0
But what if you turn all the messages on during testing
Are you assuming that errors only ever happen in Testing and not in Production?
If so, that is a very risky assumption to make...
 
Upvote 0
I used to use a small helper function:

PSEUDOCODE:
Code:
Sub Run_Query(qry)
	DoCmd.SetWarnings False
	DoCmd.RunQuery(qry)
	DoCmd.SetWarnings true
End Sub

Then I can call my one liner:
Run_Query(qry)

and so on for a few other docmd functions.

But this was idiosyncratic in the sense that I was aware of the difference between Run_Query() and DoCmd.RunQuery(), whereas to others it might not be obvious that they are not the same thing (exactly). Still you might like that workaround where you can still have the fine grained control of warnings and also use one liners in your code.
 
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,274
Members
452,628
Latest member
dd2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top