[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to capture errors in Visual Basic

Posted on 2006-06-06
15
Medium Priority
?
403 Views
Last Modified: 2013-11-25
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
0
Comment
Question by:frankmorrison
15 Comments
 
LVL 8

Expert Comment

by:List244
ID: 16848234
Frank, nobody can give you a full example, it sounds too much like homework.
0
 
LVL 8

Expert Comment

by:List244
ID: 16848236
Do you know how to write to a file?  Do you know how to do error-testing?  Where do you need help?
0
 
LVL 1

Author Comment

by:frankmorrison
ID: 16848239
What do you mean by homework?
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 8

Expert Comment

by:List244
ID: 16848256
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
 
LVL 1

Author Comment

by:frankmorrison
ID: 16848278
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
 
LVL 8

Expert Comment

by:List244
ID: 16848302
Open "C:\Documents and Settings\Visualcode\Desktop\test.log" For Append As #1
    Print #1, ," INFORMATION TO ADD TO FILE HERE"
Close #1
0
 
LVL 8

Expert Comment

by:List244
ID: 16848307
Something like that where of course you put your file to open in the quotes.
0
 
LVL 1

Author Comment

by:frankmorrison
ID: 16848315
so, would it be Print #1, , Err ?
0
 
LVL 8

Expert Comment

by:List244
ID: 16848316
Sorry should be:

Open "Test.log" For Append As #1
    Print #1, " INFORMATION TO ADD TO FILE HERE"
Close #1
0
 
LVL 8

Expert Comment

by:List244
ID: 16848321
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
 
LVL 1

Author Comment

by:frankmorrison
ID: 16848331
"information to add to file here" should be Error.Description? or what?  This is what I am not familiar with.
0
 
LVL 8

Assisted Solution

by:List244
List244 earned 1400 total points
ID: 16848351
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
 
LVL 8

Expert Comment

by:List244
ID: 16848355
That way you get your error number, description as well as the function in which got the error.
0
 
LVL 1

Expert Comment

by:woozlewight
ID: 16850532
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
 
LVL 26

Accepted Solution

by:
EDDYKT earned 600 total points
ID: 16851390
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

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question