Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error Handler Error!!! 2

Status
Not open for further replies.

thetambarineman

Technical User
Feb 29, 2000
63
0
0
GB
Having some problems with my code-
can anyone suggest whats going wrong with it??

the error handler calls this sub and passes some global variables - errnum, errdescript & errsource

Whats happening is that when an error does occur i get a run time error 55 - file already open

-can anyone suggest a way to get around this??

Public Sub sendbugreport()

Open "C:\Program Files\Agswe\System Files\errorlog.txt" For Append As 1

Print #1, errnum & " " & errdescript & " " & errsource & " " & Now

Close #1

Set mailapp = CreateObject("Outlook.application")

Set email = mailapp.createitem(mailitem)

Set recepient = email.Recipients.Add("Paul.mclornan@btinternet.com")

email.Subject = "AGSwe code error..."

email.body = "An error has occurred in the program code.. Please send this to the developers to help future updates.."
Set attach = email.attachments.Add("C:\program files\agswe\system files\errorlog.txt")

email.Display

End Sub

Thanks in advance...

Paul
 
Have actually managed to fix this one myself-and it works great.. I've used the File system object rather than the ordinary append function in VB..

This is the code that ive developed..

Public Sub sendbugreport()
Dim fso As New FileSystemObject

'for the minute just use the msg box-but future update it to the genie.
reply = MsgBox("An error has occurred in the program!!" & vbCr & "The application will resume after this message" & vbCr & "However you may have to retry an operation" & vbCr & "Do you wish to send a bug report to the developers?" & vbCr & _
"This will help in subsequent versions!!", vbInformation + vbYesNo, "Program error...")
If reply = vbYes Then

Set fso = CreateObject("scripting.filesystemobject")
'set the log file to be opened..
Set logfile = fso_OpenTextFile("C:\program files\agswe\system files\errorlog.txt", ForAppending)
'write the error details to the log file..
logfile.WriteLine errnum & " " & errdescript & " " & errsource & " " & Now
'close the log file..
logfile.Close
'set the outlook object..
Set mailapp = CreateObject("Outlook.application")
'set up and email..
Set email = mailapp.createitem(mailitem)
'add the recepient.. - me
Set recepient = email.Recipients.Add("Paul.mclornan@btinternet.com")
'set the subject of the e-mail..
email.Subject = "AGSwe code error..."
'set the body of the e-mail.. give the user an idea of what is happening...
email.body = "An error has occurred in the program code.. Please send this to the developers to help future updates.."
'now attach the log file for posting to the developer..
Set attach = email.attachments.Add("C:\program files\agswe\system files\errorlog.txt")
'now display the e-mail - all the user has to do is press send..
email.Display


Else
Resume

End If

End Sub
 
Hi,

Thanks for sharing the knowledge
By the way where do -
errdescript and errsource - these variables acquire values ?


Thank you very much.

RR


 
It may well have happened because you were using For Append As 1. It's always better to use freefile? to get a filehandle. What typically happens is that you are debugging away and interrupt the programme after it has opened the file, but before it has closed it. When you next try to use filehandle 1, it's not available. But you fixed it anyway, and got a star.

Peter Meachem
peter@accuflight.com
 
festivista97:

No problems-i've had plenty of help from people in this forum so im happy to give something back.

The variables errnum, errdescript etc are declared globally
on each sub the following would happen:

sub dosomerandomthing()

on error goto errhandler:

'invoke an error
s=3/0
'this will bring about a run-time error - 5 i think-'division by zero

exit sub

errhandler:

errnum = err.number
errdescript = err.descript
errsource = err.source

end sub

those variables are from the err (error) library.
The best thing to do is experiment around with this kind of thing...

Ill send my visual basic file if u want with a more detailed look at things-because it would take up to much space here sticking all the code in..If your interested you can contact me at: paul.mclornan@btinternet.com

If you need any further help iLL help as much as i can and the remainder of the people here at the forum can fill in the immense gaps in my knowledge!

Paul....



 
Hi Paul
Thank you very much again... Actually, why do we need a set of global variables ? What's not correct in using the Err Library values directly ? Thank you very much.

RR


 
From memory-as ive finished this project-but intend to use it in other projects-i had around 10 forms in the project dealing with a number of things. The error handler showed a different form allowing the user to send a bug report or resume program operation. When you switch forms as far as i remember the err values become erased-im not sure about this-you'd have to ask some more seasoned coders but try it out and see. The way i seen around this was to declare it globally.

Each to their own! I see programming as a challenge-to work around problems that arise..As the forums show people do things much differently!

Hope this has helped..

Paul.
 
Thank You petermeachem! I was going to mention the use of a hardcoded file handle (generally considered a BIG nono), but you already got to it!
You are right . . . you should never use a hard coded filehandle (why all of the MS example show it as hardcoded, I'll never know). You should always use the FreeFile command to get the next available file handle. - Jeff Marler B-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top