none
Importance difference between GoTo and Resume for simple error handling? RRS feed

  • Question

  • I've inherited an Access database with code like this:

    Private Sub btnValidateAddress_Click()
        On Error GoTo Err_btnValidateAddress_Click
        
        ValidateAddress
    
    Exit_btnValidateAddress_Click:
         DoCmd.Hourglass False
        Exit Sub
    
    Err_btnValidateAddress_Click:
        Call ErrorLog(...)
        Resume Exit_btnValidateAddress_Click
    End Sub
    However, some subroutines will replace the Resume with a GoTo. I've learned that Resume will clear the error object where GoTo is simply a jump. The examples I found online have more complicated Exit code where I simply need to exit the subroutine. So, for my use case, is there a meaningful difference between GoTo and Resume? Will using GoTo here disrupt future error handling? I'm thinking that the Err object will get overwritten anyway by the next error so it shouldn't matter which I use, right?

    Wednesday, December 4, 2019 4:59 PM

Answers

  • Your sample code is the On Click event procedure of a command button. Presumably, this procedure will not be called from other code, so in this specific example, it doesn't really matter whether you use

    Resume Exit_btnValidateAddress_Click

    or

    GoTo Exit_btnValidateAddress_Click

    But let's assume that the ValidateAddress procedure that is called from btnValidateAddress_Click has a similar structure. If the next to last line contains Resume, the error will be cleared, so no error is passed to btnValidateAddress_Click. Hence, btnValidateAddress_Click exits normally.

    But if the next to last line of ValidateAddress contains GoTo, the error is not cleared, so it is passed to btnValidateAddress_Click. This causes ErrorLog to be called.


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    • Marked as answer by jmt_ipa Thursday, December 5, 2019 12:58 PM
    Wednesday, December 4, 2019 9:09 PM