Hiding Warning messages

Typically when you run an action query — whether from Access or VBA — you get a warning message about what the query is about to do. That gives you a chance to change your mind before the query executes. However, when you're running action queries from VBA, you might want them to just "do their thing" without displaying any warnings or asking the user for permission.

The DoCmd object provides a simple means of hiding those warning messages. To prevent a warning message from appearing when your code runs an action query, place the following line anywhere above the line that runs the action query:

DoCmd.SetWarnings False

To get warning messages back to normal after the query runs, use this statement in your code:

DoCmd.SetWarnings True

The following example shows the sample procedure from the end of the preceding section with appropriate code added to hide warning messages just before the query runs and then set the warnings back to normal:



'Hide warning messages presented by action queries.

DoCmd.SetWarnings False

'Set SentWelcome field to True for all Houston addresses.

DoCmd.RunSQL "UPDATE [Address Book] SET SentWelcome = True WHERE


'Get warning messages back to normal.

DoCmd.SetWarnings True



You might be wondering whether select queries show warnings, because they don't change data. The answer is a definite no. In fact, if you just run a select query by using DoCmd.RunSQL in code, absolutely nothing happens onscreen. That's because, to use select queries in VBA, you have to store the results of the query in a recordset. We talk about how recordsets work in Chapter 8. In this chapter, we stay focused on action queries (and SQL statements) that make changes to data stored in tables.

Was this article helpful?

0 0

Post a comment