Easy Code Debugging - prompt_err Function

Updated jhai_salvador 0 Tallied Votes 1K Views Share

Hi! I just want to share you this code snippet of mine which I use always in my application.

This code snippet will help you debug your code easily. It will also help you locate where the error occurred and it also create an Error log located on your project directory.

In Visual Basic, Erl, a Visual Basic (undocumented in Visual Basic 4, 5, and 6 but present in all versions of Visual Basic thus far) "global variable," gives you access to the line number of any erroring line of code. So by using line numbers and by using Erl in your error handlers, you can determine which line of code erred-wow! What happens to Erl if you don't use line numbers? it will always be 0.

More details

You can use the attach file that I created my self to Add and Remove Line Numbers in your code.

You can use the prompt_err function with line number in your code like this.

Sub Update_Details()

On Error GoTo errHand

1 If rs.EOF = False Then
2    MessageBox ErrorHere
3 End If

Exit Sub
errHand:
   prompt_err Err, "Form1", "Update_Details"

End Sub

Try the function and see the result. Hope you like this one. Thanks.

'Procedure used to promp unexpected errors
Public Sub prompt_err(ByVal sError As ErrObject, ByVal ModuleName As String, ByVal OccurIn As String, Optional PrompMSG As Boolean = True)
Dim s1, s2, s3, s4, s5 As String
Dim ff As Integer

ff = FreeFile

   s1 = ModuleName
   s2 = OccurIn
   s3 = sError.Number
   s4 = sError.Description
   '* Store our line # where error Occured (always 0 when line # is not use)
   s5 = IIf(Erl <> 0, CStr(Erl), vbNullString)
   
   If PrompMSG = True Then
      MsgBox "Error From" & Chr(9) & ": " & s1 & vbNewLine & _
            "Occur In" & Chr(9) & Chr(9) & ": " & s2 & vbNewLine & _
            "Error Number" & Chr(9) & ": " & s3 & vbNewLine & _
            IIf(s5 <> vbNullString, "Line #" & Chr(9) & Chr(9) & ": " & s5 & vbNewLine, vbNullString) & _
            "Description" & Chr(9) & ": " & s4, vbExclamation, s2 & " Error"
   End If
   
   'Save the error log (The save error log will be display later on in the program)
   Open App.Path & "\Error.log" For Append As #ff
      Print #ff, "*******************************************"
      Print #ff, "Error From : " & s1
      Print #ff, "Occured In : " & s2
      Print #ff, "Date       : " & Format(Date, "MMM-dd-yyyy")
      Print #ff, "Time       : " & Time
      Print #ff, "Error #    : " & s3
      '* Check if Line # is not Null
      If s5 <> vbNullString Then
         Print #ff, "Line #     : " & s5 '* Print Line No error
      End If
      Print #ff, "Description: " & s4
      Print #ff, "*******************************************"
   Close #ff
End Sub
AndreRet 526 Senior Poster

I did not like the attachment. As far as I know, we are not allowed to post full on applications (.exe) here, Jhai. The code looks fine, I'll play a bit with it when time permits.:)

jhai_salvador 48 Junior Poster

ohh.. okay.. then I'll just make it as the source. :)

------------
Source code is added in the attachment. :) Hope you'll like it.

peter_budo 2,532 Code tags enforcer Team Colleague Featured Poster

@jhai_salvador if you want to include attachments please do it without EXE or any other executable file type as people are suspicious about this. Once you upload code people can compile it and create their own packages

jhai_salvador 48 Junior Poster

Forgot to remove it :D

here it is Again w/o the exe....

peter_budo commented: Good job +16
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.