Solved

detect docmd error even if setwarnings  is OFF

Posted on 2011-09-07
3
348 Views
Last Modified: 2012-08-14
Often, my vba programs need to analyze the cause of an error and take appropriate action.



But this is not easy when the error comes from a docmd method, when setwarnngs is off.

I want the my vba to see the EXACT same text that a user would have seen if setwarnings had been ON, but I do not want the user to see that text.
 


 For instance the following code does not work

on error resume next
docmd.setwarnings false
docmd.xxxxxxxxxxx
myerror$ = error$   '  THIS will always be blank. It fails because Access thinks that setwarnings false means "don't throw any errors".
docmd.setwarnings true
on error goto myerr_routine
if myerror$ like "*unable to append*" then ....

In the past I have used many "tricks" to get around this problem but I am looking for a general solution that will work for all the many dozens of methods that could be invoked.  



rberke
0
Comment
Question by:rberke
  • 2
3 Comments
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 500 total points
ID: 36499089
Let me suggest a safer way ... that gets away from the SetWarnings False - which can have many undesirable ramifications.  

The problem with SetWarnings False is ... if one (or more) of the queries fail for *whatever* unexpected reason ... you will never know this because SetWarnings False masks out *all* errors, which can lead to you *thinking* that everything ran ok, when in fact it did not.

The approach below has two advantages:

1) You do not get the warning prompts - just like if you use SetWarnings False ... AND ...
2) If an Error DOES occur, it will be trapped and you can act accordingly.

Private Sub btnRunQry_Click()
   
    On Error GoTo btnRunQry_Click_Error
    CurrentDb.Execute "SomeActionQuery1", dbFailOnError
    CurrentDb.Execute "SomeActionQuery2", dbFailOnError
    MsgBox "Operation completed successfully!"

btnRunQry_Click_Exit:
    Err.Clear
    Exit Sub

btnRunQry_Click_Error:
   MsgBox "An error occurred:" & vbCrLf & Err.Number & vbCrLf & Err.Description
   GoTo btnRunQry_Click_Exit
End Sub

******

Another problem with SetWarnings False is ... if you *forget* to execute SetWarnings True ... OR ... some other error does occur and SetWarnings True does not get execute ... you have a big problem ... because, False stays in effect unit you close and reopen Access.  

So, for example if you are working in design view of say a form (or table, etc) ... and you make some changes ... and hit Save ... you are NOT prompted for 'Do you want to save these changes ...". I will just silently save the changes ... which may NOT be what you wanted to do!!
0
 
LVL 5

Author Comment

by:rberke
ID: 36499331
yes, that is an alternative when I am running a query/sql statement.

But, there are a bunch of other docmds methods.  Any suggestions for those?



0
 
LVL 5

Author Closing Comment

by:rberke
ID: 36943496
The partial answer suggested an alternative that works for open query.

I will resubmit the question at a later date for alternatives to other docmd functions.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question