Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2010

    Unanswered: Proper way to bypass Error if it occurs

    I want to be sure an error doesn't show any error message if it fails. Whats the proper way to do that?

  2. #2
    Join Date
    Dec 2004
    Madison, WI
    You can use...

    On error resume next

    BUT I typically don't advise this. If you use it, make sure to put something like this at the end of the coding...
    On error goto Done
    On error goto 0

    (I personally prefer the On Error goto Done versus On error goto 0)

    Using the On error resume next can be VERY dangerous as it will eliminate any errors from showing which you don't really want to do unless absolutely needed (or until the mdb is closed and reopened). Again, if you use it, make sure to put in something as that above to re-instate error checking and make sure your code 'hits' that line of re-instating error checking. I rarely use the on error resume next due to it's dangerous potential.

    Typically, instead of the On error resume next, I'll put in On error goto SomeLabel and make it so that SomeLabel is where I want it to go.

    Function DoSomething()
    On error goto BadData
    exit function
    msgbox "Bad data was entered....."
    End Function

    Another option when updating queries and you don't want it to show a popup saying "you are about to update x records...", you can use the docmd.setwarnings false
    docmd.setwarnings false
    docmd.openquery "SomeUpdateQuery"
    docmd.setwarnings true

    Again, importantly making sure to turn the warnings back on.

    I usually do something like this when it comes to update type queries.
    Dim QI as integer
    QI = msgbox("Are you sure you want to update the recordset? (this is your last warning before updating the data)",vbYesNo+vbExclamation)
    if QI = vbno then exit sub
    docmd.setwarnings false
    docmd.openquery "myUpdateQuery"
    docmd.setwarnings true

    Just remember that the most important thing is if the code fails, you want it to go to some part of the coding which re-instates error checking again.

    For example, this could be bad:
    docmd.setwarnings false
    docmd.openquery "myUpdateQuery"
    docmd.openquery "AnotherUpdateQuery"
    some other coding...
    docmd.setwarnings true

    because if something fails in the other coding and it never hits the docmd.setwarnings true line of coding, you've then turned off warnings messages.
    Last edited by pkstormy; 04-28-10 at 12:14.
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts