Chip's page was just a general overview about handling errors
IMHO VBA's error handling leaves something to be desired
FWIW, in my test, TestErr() and TestZero() both use error = 11 (not recommended) since my error and VBA's error have the same number (i.e. 11).
Since I have vbObjectError + 11 in TestErr() they can still be told apart by the error handler, but I think it'd be confusing to have 11 used for 2 different errors
Sub TestErr() Err.Raise vbObjectError + 11, "TestErr", "This is My Error" End Sub Sub TestZero() Dim x As Long x = 1 / 0 End Sub Sub TestErr2() Err.Raise vbObjectError + 1000, "TestErr2", "You did SOMETHING wrong, so go back and fix it!!!" End SubYes. No idea why. Maybe MS is inconsistent???Plus, as mentioned, bettersolutions Codes page shows native errors of 520, 521, some 700's, and some 31000's.
520 Can't empty Clipboard 521 Can't open Clipboard 735 Can't save file to TEMP directory 744 Search text not found 746 Replacements too long 31001 Out of memory 31004 No object 31018 Class is not set 31027 Unable to activate object 31032 Unable to create embedded object 31036 Error saving to file 31037 Error loading from file
vbObjectError+515 = -2147220989If i'm supposed to add vbObjectError, and if 513-65535 is usable, then why does `Err.Raise vbObjectError+515` give me a native error!?
and
-2147220989 - vbObjectError = 515
-2147220989 is a 'native' error based on the MS does things but the " - vbObjectError" gives you back the original 515 so you know it's in the user error range
The error handling code from the MS doc can be used to determine if it was a user generated application error or a system automation error
Like I said, the error handling process leaves a lot to be desired (another IMHO)
However, I normally use error handling to catch the error (either VBA's or one of mine), determine what it is, and take corrective action. The actual description or type isn't that important TO ME.
Select Case errornumber statements to branch to the appropriate correction / error message / etc. after i find out if it's an application error or an automation error (normally it's mine)
HANDLER: ' First, strip off the constant added by the object to indicate one of its own errors. MyErr = Err.Number - vbObjectError 'If you subtract the vbObjectError constant, and the number is still ' in the range 0-65,535, it is an object-defined error code. If MyErr > 0 And MyErr < 65535 Then Select Case MyErr Case 11 MsgBox "You should leave system error numbers alone" Case 1000 MsgBox MyErr & " -- " & Err.Source & " -- " & Err.Description Case Else MsgBox "Something happened" End Select ' Otherwise it is a Visual Basic error number. Else MsgBox Err.Number & " -- " & Err.Source & " -- " & Err.Description End If Resume Next





Reply With Quote
