Enumerate Your Errors

Enumerate Your Errors

To keep a list of available error codes in your components, declare a private enumeration:

 Private Enum Errors	InvalidUserID = vbObjectError + 513	InvalidPassword	SearchNotFoundEnd Enum

Setting the first value sets the seed number for all subsequent items in the list, each one incrementing by one. (Microsoft recommends starting at “512 plus 1” above vbObjectError.)

Now you won’t have to remember error numbers throughout your code. Simply raise your errors like this:

 Err.Raise Errors.InvalidUserID, "Login", "Invalid UserID"

When you type the enumeration name Errors, VB pops up a list of available choices. Be careful, though, not to add new items in the middle of the list, because the value of all entries below the new item increases by one. This can cause the parent of the object to handle errors incorrectly because the error numbers will be different. You can avoid this by specifying exactly what value you want for each Enum, instead of relying on the default increment.


Share the Post: