VB icon

ErrorExample.vbp

Email
Submitted on: 1/3/2015 6:47:00 AM
By: Timothy Vanover (from psc cd)  
Level: Not Given
User Rating: By 2 Users
Compatibility: VB 5.0, VB 6.0
Views: 740
 
     This is an example of how to use the intrinsic Visual basic App object to log errors to a file, which may later be helpful in debugging an application. The log file has the error number, error description, form name, sub or function name, and a date-time stamp when the error occurred. This also limits the error so that multiple loggings of the same error are not recorded in iterations.
 

Windows API/Global Declarations:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
'Windows API/Global Declarations for :ErrorExample.vbp
'**************************************
'* this code goes in a module for public use
'* Timothy A. Vanover 
'* hdhunter@home.com
Public Sub LogErrors(lngErrNumber As Long, strErrDescription As String, _
strErrModule As String, strErrProcedure As String)
'*use to prevent recording multiple errors in loop 
 Static LastErrorRecorded As String 
 
 
 If CStr(Err.Number) & Err.Description & strErrModule & strErrProcedure = LastErrorRecorded Then
 Exit Sub
 Else
 
 App.StartLogging App.Path & "\" & App.EXEName & ".log", vbLogToFile
 App.LogEvent vbCrLf & _
 "Error Description: " & strErrDescription & vbCrLf & _
 "Error Number: " & lngErrNumber & vbCrLf & _
 "Module Name: " & strErrModule & vbCrLf & _
 "Procedure Name: " & strErrProcedure & vbCrLf & _
 "Version Number: " & App.Major & "." & App.Minor & "." & App.Revision & vbCrLf & _
 "Date: " & Format$(Now, "Short Date") & vbCrLf & _
 "Time: " & Format$(Now, "Long Time") & _
 vbCrLf & vbCrLf
 
 LastErrorRecorded = CStr(Err.Number) & Err.Description & strErrModule & strErrProcedure
 End If
 
End Sub
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: ErrorExample.vbp
' Description:This is an example of how to use the intrinsic Visual basic App object to log errors to a file, which may later be helpful in debugging an application. The log file has the error number, error description, form name, sub or function name, and a date-time stamp when the error occurred. This also limits the error so that multiple loggings of the same error are not recorded in iterations.
' By: Timothy Vanover (from psc cd)
'
' Inputs:The public sub in the moderror.bas takes 4 parameters 
LogErrors err.number, err.description, me.name , "MySubNameHere"
'
' Returns:This writes a .log file which may be opened with any text editor for examination as to the general area of an error that could crash your program.
'
' Assumes:The app.StartLogging will not work in the development enviroment. The code must be in a compiled executable for it to work.
'**************************************

'* This must be compiled into an executable for the intrinsic 
'* error logging to work
'* It will not work from the development enviroment.
'* paste this code on to a form, save and compile it for the demo 
Private Sub Form_Load()
'*here is an example of a sub which I raise errors in for the demo
 ErrorDemoSub
 
 MsgBox "Errors Recorded in Error Log File"
 
 Unload Me
 
End Sub
Private Sub ErrorDemoSub()
 Dim i As Integer
 Dim ii As Integer
 On Error GoTo MyErrorLog
 'we'll simulate an error in a loop although we only log it one time
 For i = 1 To 20
 For ii = 1 To 5 
Err.Raise i
 Next ii
 Next i
 
 Exit Sub
 
MyErrorLog:
 LogErrors Err.Number, Err.Description, Me.Name, "ErrorDemoSub"
 Err.Clear
 Resume Next
 
End Sub


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Not Given category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.