How to capture errors in Visual Basic

Can anyone give me a working example on how to create a log file that will capture any errors when the application is executed?

I will only give points to full working example.

Thanks
LVL 1
frankmorrisonAsked:
Who is Participating?
 
EDDYKTCommented:
i will create a subroutine in module and you can call from anywhere from your program


Private Sub INSERTLOG(byval routine as string, byval msg As String)
    Dim OneLine As String, FileNo As Integer
   
    On Error Resume Next
    FileNo = FreeFile
    Open "c:\log.txt" For Append Shared As #FileNo
    Print #FileNo, Now & " >> " & routine & " >> " & msg
    Close #FileNo
End Sub

To call


Private Sub Form_Activate()
On Error GoTo errorhandler

' I have a bunch of code here, too long to include

Exit Sub

errorhandler:
INSERTLOG "Form_Activate", Err.description & "(" & err.number & ")"
End

End Sub

0
 
List244Commented:
Frank, nobody can give you a full example, it sounds too much like homework.
0
 
List244Commented:
Do you know how to write to a file?  Do you know how to do error-testing?  Where do you need help?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
frankmorrisonAuthor Commented:
What do you mean by homework?
0
 
List244Commented:
I mean you have an exact want, and you need it in full.  If you need help, we can help, but you should ask more what you
are having trouble with.  It sounds like homework as you are asking for a complete application which proves to be nothing
more than a show of knowledge on an area.  That is the kind of thing homework assignments ask for.


Also, you want a program which captures all errors and saves them to a log file in full-source?  We would have to write a
complete program including errors to give you this.  It would be much simpler if you actually told where you need help.
0
 
frankmorrisonAuthor Commented:
First of all I am not a student.  Second, I think I was very clear where I need help with, but I will gladly give more details:  In my vb app, I have the following:

Private Sub Form_Activate()
On Error GoTo errorhandler

' I have a bunch of code here, too long to include

Exit Sub

errorhandler:
' <============= I want to write to a log file here
End

End Sub



0
 
List244Commented:
Open "C:\Documents and Settings\Visualcode\Desktop\test.log" For Append As #1
    Print #1, ," INFORMATION TO ADD TO FILE HERE"
Close #1
0
 
List244Commented:
Something like that where of course you put your file to open in the quotes.
0
 
frankmorrisonAuthor Commented:
so, would it be Print #1, , Err ?
0
 
List244Commented:
Sorry should be:

Open "Test.log" For Append As #1
    Print #1, " INFORMATION TO ADD TO FILE HERE"
Close #1
0
 
List244Commented:
Just one comma sorry, used to binary files, those use two.  Two in a normal will put a tab, you don't want that.
But yes like so:

Print #1, "VariableOrText"
0
 
frankmorrisonAuthor Commented:
"information to add to file here" should be Error.Description? or what?  This is what I am not familiar with.
0
 
List244Commented:
I would do something like this:

Private Sub Command1_Click()
Dim Where As String
Where = "Command1"
On Error GoTo 1
Dim temp As String
temp = "Error Here"
For i = 0 To Len(temp)
    Form1.Caption = Mid(temp, i, 0)
Next i
Exit Sub
1
Dim Error As String
Error = Where & ":" & Err.Number & " - " & Err.Description
Open "Log.txt" For Append As #1
    Print #1, Error
Close #1
End Sub
0
 
List244Commented:
That way you get your error number, description as well as the function in which got the error.
0
 
woozlewightCommented:
Why not use the windows application log. This can be accessed my right clicking on my computer > Manage > Event Viewer > Application. Some remote support programs also email out application log errors making support easy.

To write to the application log it needs to be loaded with the splash or first form. To do this add the folowing code into the form load:

call app.startlogging("",vblogtoNT)

if using windows95 change this to:
call app.startlogging("",vbAutoLog)

Now that we have started the service we need to add the code. The application log has an icon, date, time, source etc.

To write into the log simply enter the following code:

call app.logEvent("Your text Message " & err.description, vblogeventerror)

This creates a critical icon in the event viewer
To create an information icon change the type to  vblogeventinformation
To create the icon as a warning change the type to  vblogenveventtypewarning

To use this as an error handler put in the following

'################################################
 on error goto errhandle

' your code

exit sub

errhandle:

call app.logEvent("Your text Message " & err.description, vblogeventerror)
msgbox err.description, vbinformation ' show your error to your client

'################################################

My tip is to put in the name of your subroutine in the "Your text Message part" as you therefore know which routine caused the error.

In addition it should be noted that you cannot use the applogevent in the vb6 compiler. To test your code you need to compile the program and run it without vb6 loaded otherwise it will not work!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.