Handling Errors in VB/VBA/VBS/ASP

Creating your own errors

"The cheat code to error handling", as I call it.

The essential part about adding your own structure to error events, is the Raise event handle, but first of all let's discuss the structured requirements a bit.

One case where you may wish to use your own error definition could be the classical example of validating a textbox to only contain positive numerical values.

Assume you have a form1 and a text box named txtInteger.

Place the following in the form source code

Private Sub txtInteger_LostFocus()
If IsNumerical(txtInteger.Text) = False Then
    MsgBox "The value is not an integer"
Else
    MsgBox "The value is an integer"
End If
End Sub


Just remember that it is not recommendable to add this to the Change event of the text box it self. This due to that the event would be initiated by the user at the slightest change and this may be very annoying. Thus adding it to the LostFocus event brings more a user-friendly action to the code.

Instead of having a message box pop up with declarations for every case there is an event driven error, programmatically added by you to your code, you could make use of the err object in a more refined way.

This also enables you to add your own help files, help context ID's and so on, to even better provide a more professional way of displaying help and errors to users.

Now lets alter the previous code to force your own error processing instead of using messages boxes in the code.

Private Sub txtInteger_LostFocus()
On Error GoTo ErrHandler
Dim ErrMsg As String 'set a string variable to enable display of errors

If IsNumerical(txtInteger.Text) = False Then

' raise the error that we select to number 65000
' remember here that the error numbers may be taken
' and recall the above notes form the MS VB help file of their ranges

' the second value is the programmatic project ID
' since this is optional and will be automatically set to the current application ' we can leave this as a Null string

' The third message line is the Err.Description handle
' this should not be omitted since if windows located another error with ' a similar number, that error description would take over.

' The fourth value is also omitted, it is then evaluated to the general VB help file

' The final value is the Context ID,
' this is also omitted but we set this to 0 to clean up the code

Err.Raise 65000, "", "The added value in the text box was not a valid integer", "", 0

End If

Exit Sub ' exit the sub here (just as we did with the functions earlier)
ErrHandler:
Select Case Err.Number
Case 65000
' if our error occur, then set the error message to the values above
   ErrMsg = "An error occurred in the application" & vbCrLf & _
"Error Number " & Err.Number & vbCrLf & _
"Description: " & Err.Description, vbCritical, "Error!"
   Err.Clear
Case Else
' if you omit any code at all here, the regular error processing would take over.
' to beautify the code you should add the following here.
' (otherwise the ErrMsg would be empty)

   ErrMsg = "An error occurred in the application" & vbCrLf & _
"Error Number " & Err.Number & vbCrLf & _
"Description: " & Err.Description & vbCrLf & _
"Please se help on this in " & Err.Helpfile, vbCritical, "Error!"
   Err.Clear
End Select
' if the error is trapped display it by using this message box MsgBox = ErrMsg
' Resume at the location of code interruption Resume Next End Sub


You have now created your first assigned error number, 65000.

To further enhance this code, you could of course dedicate a complete module to error handling alone. Thereby leaving room for assigning a huge array of error trapping, descriptions and other detailed information.

One thing well worthy of mentioning here is that you have to assign context ID to the error numbers when you reference a help file in your project. The Err.HelpContext Is a Long value assigned by a constant. It could for instance be as follows in our case; Const Context65000ID = 35756 where the number 35756 refers to the context ID in the help file you have to include.

Information on how to declare constants and use them appropriately are provided in the VB help files but I will include a simple example here for you. To declare the constant mentioned above, paste the following code in the uppermost part of the source code file. This is referred to as the Declaration area.

[Public|Private] Const Context65000ID As Long = 35756

As you see, the declaration of constants is not that different from the one of declaring any other variable. The main difference is that Constants are always declared PRIOR to entering a Function or Sub. Constants can be used anywhere in your code, once declared and assigned a value, and hold the scopes as any other function or variable can. You also have to set the VarType before assigning a value to the constant.

Constants can never hold relative references. Thus meaning you can not assign a text constant the relative path reference as in App.Path & "\yourfile.txt". It has to be absolute values ONLY.

Please study the help files for the Err.Raise event closer and see the provided sample source to find out more details about the use of help files and context ID's.

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“It is practically impossible to teach good programming style to students that have had prior exposure to BASIC. As potential programmers, they are mentally mutilated beyond hope of regeneration.” - E. W. Dijkstra