Error Messenger Routine VB6

What I have is a class module that I put in every program I write. One of the things it does is handle errors. It will show a message box, if needed, and write the error to a text file in the App.Path. If you have a lot of errors, keep an eye on this file. It will add an entry every time the routine is called.

The thing I like about this is:

  1. The routine will save the Error Number and Error Description from VB
  2. The Form the error occured (you have to tell it this), and the sub the error occured (also have to tell it this) will also be saved
  3. A description you can add telling where in the code the error occured, or what the program was about to do before the error occured.

Here is the code, put this in a global module so any form can access it.


Public Sub ErrorMessenger(pNumber As Long, pDesc As String, _
    pModule As String, pProcedure As String, pMsg As String)

' useswitch is a global boolean variable - set to true if the program
' was started at the command line using a switch that does not display
' any forms, if there is a GUI, then set it to False.  When it is set to False,
' a message box will be displayed to the user, if it's set to True, no 
' message box is displayed, but the error is still logged

' All variables except pNumber are strings, pNumber is Long
' pNumber is the error number (Err.Number)
' pDesc is the error description  (Err.Description)
' pModule is the module (form, module, class) where the procedure is
' pProcedure is the sub routine the error occured
' pMsg is any other message that needs to be saved or displayed

' ErrorMessenger syntax:
' ErrorMessenger Err.Number, Err.Description, "Module1", "ErrorMessenger", tmpMsg
' where tmpMsg is any other message you want displayed (pMsg)

Dim tmpErrmsg As String
Dim tmpFileNum as Integer

tmpFileNum = FreeFile

  tmpErrmsg = "Open RunError.log"
Open "RunError.log" For Append As tmpFileNum

  tmpErrmsg = "Write Error Log Time"
Print #50, "Error Logged at " + Time$ + " on " + Date$

  tmpErrmsg = "Write Where Error Occured"
Print #50, "Error occured in '" + pModule + "', in Procedure '" + _
    pProcedure + "'"

  tmpErrmsg = "Write Error Eumber and Description"
Print #50, "Error Number " + Str$(pNumber) + " - " + pDesc

  tmpErrmsg = "Write tmpMsg and CurrentChart variable"
Print #50, pMsg

  tmpErrmsg = "Write Space Between Error Messages"
Print #50, "==============================================================" + vbCrLf

  tmpErrmsg = "Close RunError.log"
Close tmpFileNum

If UseSwitch = False Then
  tmpErrmsg = "Display Error Message"
  MsgBox ("Error - " + Str$(pNumber) + " occured in " + pModule + _
    " in Procedure " + pProcedure + vbCrLf + pDesc + vbCrLf + vbCrLf + pMsg)
End If

Exit Sub

errhandle:

MsgBox ("Error Number - " + Str$(Err.Number) + vbCrLf + Err.Description + _
    vbCrLf + "Error Occured in 'ErrorMessenger'" + vbCrLf + tmpErrmsg)

End Sub

In every sub routine that may encounter an error (and yes, there are many) the first line of code is 'On Error Goto Errhandle'

I use a Global string (tmpMsg), defined in a module, describing what is going on in the code. For example, if I am opening a table, I include a line just before the open statement like "Opening Access Table"

If an error occures, the errhandle portion is executed.

Here is where I will call the ErrorMessenger routine

Let's say the error happened in Form1 during the Form_Load subroutine, the code would look like this:

ErrorMessenger Err.Number, Err.Description, "Form1", "Form_Load", tmpMsg

The remarks at the beginning of the ErrorMessenger routine describe the flags for the routine

It is a bit of a pain in the ass at times, adding the tmpMsg string before any suspect code, but it’s kind of like documenting the code - and thats ALWAYS a good idea. You always know what the code is SUPPOSE to do next. This has proved very helpful for me, I hope it helps you.



BACK



Site last updated January 31, 2004