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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error in an error handler 2

Status
Not open for further replies.

jopaumier

Programmer
Mar 15, 2002
97
US
I have a sub in which I have an error handler in the event of a problem with some database manipulations. What happens when there is an error within my error handler? For example I am adding a record to a table that identifies the source of my error, but if I've made a typographical error of a field in the table, then I've introduced an error in the handler? Does that error bubble up or does the program crash? I've not seen this aspect of error handling addressed, and from my experience it seems like it crashes, but it may be that I do not have an error handler in the procedure (and above) calling the sub.

Jim
 
How do you trap your errors? Do you use on error for each sub or for a whole form? You can place the error handler in a module and then call it from anywhere within your app. In your error handler you then need to include something along the lines of on error resume next.

This will not record this error into your database though, but will stop your app from crashing or entering a loop. Search this forum for error handling as I think strongm posted a superb example about a month back

Good luck

BB
 
BB,

Thanks for the reply. The error handler is in a sub within a class module. I don't think I've programmed the error handling as efficiently as I should, but I had to do what I knew at the time. Thanks for the suggestion for strongm's post. I'll take a shot at finding it.

Jim
 
Sorry to hop in, but I also am in the same boat. I have just used MZTools to add an error procedure in each Sub. I am wondering how near I am from a disaster just relying on it to produce a messagebox error number. The code does nothing to include Resume next. eg below

Private Function IsDriveReady() As Boolean

Dim fso As New FileSystemObject
On Error GoTo IsDriveReady_Error

IsDriveReady = fso.GetDrive("A:").IsReady

On Error GoTo 0
Exit Function

IsDriveReady_Error:


MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure IsDriveReady of Form Form2"

End Function


Any ideas of how I would insert a resume next, and any comments as to how near the wind I'm sailing. Thanks jopaumier for raising the thread. Hope you get something from it. All best seasonal greetings to all
 
With MZTools you can click on the options toolbar item, it looks like the VB toolbox icon, and select the error handler tab and then customize what your default error handler that is inserted looks like.
 
and by the way, what you have now isn't going to cause an error. You need to worry when for example in your ErrorHandler itself you are inserting a record into a DB to record the error but there is no connection to the db.
 
Zor,

Resume should be used to clear the error after handling it otherwise handlers further up the call stack won't function correctly.

Private Function IsDriveReady() As Boolean

Dim fso As New FileSystemObject
On Error GoTo IsDriveReady_Error

IsDriveReady = fso.GetDrive("A:").IsReady

GoTo ExitFunc

IsDriveReady_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure IsDriveReady of Form Form2"
Resume ExitFunc

ExitFunc:

End Function

Paul Bent
Northwind IT Systems
 
Thankyou both, that was quick. Usually Fridays its quiet. I feel a bit happier now and know the main area to be looking for handling. I will relook at MZTools customization options. Have a star each, thanks again.
 
Zor, no need to be sorry for hopping in. Glad to see that I am not the only one who has scratched his/her head over this.

DrJavaJoe, you are exactly right about inserting a record into a db to record the error. I join two tables then check for widows and orphans. If something goes wrong with the check for, say, orphans, I drop to an error handler. My usual scenario is that I've spelled something wrong in the error handler in which I am trying to report out a problem with the check.

Paul, could you use err.clear to clear the error, or are there differences between resume and clear that I am not aware of? Also if there are multiple lines of code in the error handler that are working with a database (and not just a message box), where would resume and/or clear go? Shouldn't the Resume appear first so if I've typed something wrong, the program won't do the unexpected. (Hmm maybe I just answered my own question.) Of course the user might not know an error occurred if nothing gets written to the database.

Jim
 
jopaumier,

Err.Clear will clear the error and resume execution at the next line. Resume Next | Label clears the error and resumes at the line following the one that raised the error or directs execution to a specific location. You can use whichever is appropriate for the logic of your error handler.

Paul Bent
Northwind IT Systems
 
Thanks Jim, appreciated that. I must look more into error handling, mainly when something goes wrong when other users try to access and .Update records on tables. Using Access. Many thanks again all.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top