Solved

detect docmd error even if setwarnings  is OFF

Posted on 2011-09-07
3
344 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 - Access MVP) 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now