473,722 Members | 2,325 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Error trapping code

I use code extensively; I probably overuse it. But I've been using
error trapping very sparingly, and now I've been trapped by that.

A form that works for me on the system I'm using, apparently runs into
problems on the system where it will actually be used, and since I
used so little error-trapping it dies very ungracefully.

I will of course try to fix whatever is causing the error and add
error-trapping to the functions where the problem surfaces, but I
realize that I need to add error-trapping code with a more liberal
hand.

My question is, therefore, should error-trapping code bookend every sub
and function? If not, what criteria do I use to decide whether to add
it or not?
--thanks, thelma

Nov 13 '05 #1
13 4480
Yes, if the application matters, use error-trapping in every sub and
function in your application.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Thelma Lubkin" <th****@alpha2. csd.uwm.edu> wrote in message
news:df******** **@uwm.edu...
I use code extensively; I probably overuse it. But I've been using
error trapping very sparingly, and now I've been trapped by that.

A form that works for me on the system I'm using, apparently runs into
problems on the system where it will actually be used, and since I
used so little error-trapping it dies very ungracefully.

I will of course try to fix whatever is causing the error and add
error-trapping to the functions where the problem surfaces, but I
realize that I need to add error-trapping code with a more liberal
hand.

My question is, therefore, should error-trapping code bookend every sub
and function? If not, what criteria do I use to decide whether to add
it or not?
--thanks, thelma

Nov 13 '05 #2
rkc
Thelma Lubkin wrote:
My question is, therefore, should error-trapping code bookend every sub
and function? If not, what criteria do I use to decide whether to add
it or not?


Functions, methods that return a value to a calling procedure, don't
necessarily need error handling, but the calling code certainly should
have it. Objects(class modules) should trap an error and pass it on to
the client code.


Nov 13 '05 #3
Is there an easy way to insert basic error trapping into all subs after the
fact? Just the basic : msgbox err.number & err.description stuff
"Allen Browne" <Al*********@Se eSig.Invalid> wrote in message
news:43******** *************@p er-qv1-newsreader-01.iinet.net.au ...
Yes, if the application matters, use error-trapping in every sub and
function in your application.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Thelma Lubkin" <th****@alpha2. csd.uwm.edu> wrote in message
news:df******** **@uwm.edu...
I use code extensively; I probably overuse it. But I've been using
error trapping very sparingly, and now I've been trapped by that.

A form that works for me on the system I'm using, apparently runs into
problems on the system where it will actually be used, and since I
used so little error-trapping it dies very ungracefully.

I will of course try to fix whatever is causing the error and add
error-trapping to the functions where the problem surfaces, but I
realize that I need to add error-trapping code with a more liberal
hand.

My question is, therefore, should error-trapping code bookend every sub
and function? If not, what criteria do I use to decide whether to add
it or not?
--thanks, thelma


Nov 13 '05 #4
See:
www.mztools.com

They have a set of tools you can download for VBA without charge. It
installs a toolbar that (amongst other things) lets you insert your
procedure stub and/or error handling with a single mouse click in any
procedure, including references to the name of the procedure if you wish.
That's what I use.

It handles the MsgBox stuff if you wish. What I do is to get it to call a
generic error-handing procedure, passing in the value of Err.Number,
Err.Description , and other information such as the name of the procedure
that caused this error, the name of the module, any parameters that I am
aware of at design time that I might want, and a flag to indicate whether to
show this message to the end user or not.

There are some major advantages to calling a single procedure like that:
a) If I decide to change the way the error handler works, there's just one
piece of code to update.

b) I can handle some errors in a generic way, such as the many errors that
can indicate inability to save a record at the moment, or the 2501 you
receive when a user or NoData event cancels opening a report etc.

c) I can log the errors. In my first Access database (version 1), I asked
users to write the error message down. They never did. It occured to me to
record the errors in a table. Just a couple of times a year, this proves
really useful, especially to debug something that is happening on one
machine and not another. If you want more info on how to do that, copy it
from:
Error Handling in VBA
at:
http://allenbrowne.com/ser-23a.html

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"John Welch" <j+ohnw+elch@ca l+central.com (remove +'s)> wrote in message
news:df******** *@enews2.newsgu y.com...
Is there an easy way to insert basic error trapping into all subs after
the fact? Just the basic : msgbox err.number & err.description stuff
"Allen Browne" <Al*********@Se eSig.Invalid> wrote in message
news:43******** *************@p er-qv1-newsreader-01.iinet.net.au ...
Yes, if the application matters, use error-trapping in every sub and
function in your application.

--
"Thelma Lubkin" <th****@alpha2. csd.uwm.edu> wrote in message
news:df******** **@uwm.edu...
I use code extensively; I probably overuse it. But I've been using
error trapping very sparingly, and now I've been trapped by that.

A form that works for me on the system I'm using, apparently runs into
problems on the system where it will actually be used, and since I
used so little error-trapping it dies very ungracefully.

I will of course try to fix whatever is causing the error and add
error-trapping to the functions where the problem surfaces, but I
realize that I need to add error-trapping code with a more liberal
hand.

My question is, therefore, should error-trapping code bookend every sub
and function? If not, what criteria do I use to decide whether to add
it or not?
--thanks, thelma

Nov 13 '05 #5
Thelma, an example might help to explain rkc's suggestion.

Say you have a procedure that calls another proc like this:
Sub MySub
Debug.Print MyFunction()
End Sub
Function MyFunction()
MyFunction = 6
End Function

Now if you add error handling to just MySub, and an error occurs when it
calls MyFunction, the error handler in MySub will catch the problem when it
calls MyFunction and the problem occurs.

While that's true, I prefer to add error handling to MyFunction anyway. The
main reason is that I find it much easier to track down an error if I know
what procedure caused it. Particularly when you get one proc that calls
another that calls another ..., I'm too lazy to try to track down which one
actually caused the error, when I can get Access to gell me just by using
error handling in each one. The other reason is that later on I may need to
call MyFunction() from somewhere else, so I really prefer just to have the
error handlers in place anyway.

As a general rule, it is easier to write software than to maintain it, so
anything you can do to write maintainable code is worth the effort.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"rkc" <rk*@rochester. yabba.dabba.do. rr.bomb> wrote in message
news:N_******** *********@twist er.nyroc.rr.com ...
Thelma Lubkin wrote:
My question is, therefore, should error-trapping code bookend every sub
and function? If not, what criteria do I use to decide whether to add
it or not?


Functions, methods that return a value to a calling procedure, don't
necessarily need error handling, but the calling code certainly should
have it. Objects(class modules) should trap an error and pass it on to the
client code.

Nov 13 '05 #6
Allen Browne <Al*********@Se eSig.Invalid> wrote:
: Thelma, an example might help to explain rkc's suggestion.

: Say you have a procedure that calls another proc like this:
: Sub MySub
: Debug.Print MyFunction()
: End Sub
: Function MyFunction()
: MyFunction = 6
: End Function

: Now if you add error handling to just MySub, and an error occurs when it
: calls MyFunction, the error handler in MySub will catch the problem when it
: calls MyFunction and the problem occurs.

Actually, I don't really understand that. Suppose I have

Sub MySub
Debug.Print MyFunction(0)
End Sub
Function MyFunction(deno m as Integer)
MyFunction = 6/denom
End Function

Why does execution return to MySub so that the error can be trapped
instead of simply dying in MyFunction?

: While that's true, I prefer to add error handling to MyFunction anyway. The
: main reason is that I find it much easier to track down an error if I know
: what procedure caused it. Particularly when you get one proc that calls
: another that calls another ..., I'm too lazy to try to track down which one
: actually caused the error, when I can get Access to gell me just by using
: error handling in each one. The other reason is that later on I may need to
: call MyFunction() from somewhere else, so I really prefer just to have the
: error handlers in place anyway.

I intend to write a single function that gets offending
sub/function name, etc, as parameters, to report error messages
to the end-user, and after reading your earlier post, I think
that I'll use it to save those messages for the
developer/maintainer as well. Do you just add a table to the
database for these?

Thank you for the help. --thelma

: --
: Allen Browne - Microsoft MVP. Perth, Western Australia.
: Tips for Access users - http://allenbrowne.com/tips.html
: Reply to group, rather than allenbrowne at mvps dot org.

: "rkc" <rk*@rochester. yabba.dabba.do. rr.bomb> wrote in message
: news:N_******** *********@twist er.nyroc.rr.com ...
:> Thelma Lubkin wrote:
:>
:>> My question is, therefore, should error-trapping code bookend every sub
:>> and function? If not, what criteria do I use to decide whether to add
:>> it or not?
:>
:> Functions, methods that return a value to a calling procedure, don't
:> necessarily need error handling, but the calling code certainly should
:> have it. Objects(class modules) should trap an error and pass it on to the
:> client code.
Nov 13 '05 #7
Answers embedded.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Thelma Lubkin" <th****@alpha2. csd.uwm.edu> wrote in message
news:df******** **@uwm.edu...
Allen Browne <Al*********@Se eSig.Invalid> wrote:
: Thelma, an example might help to explain rkc's suggestion.

: Say you have a procedure that calls another proc like this:
: Sub MySub
: Debug.Print MyFunction()
: End Sub
: Function MyFunction()
: MyFunction = 6
: End Function

: Now if you add error handling to just MySub, and an error occurs when it
: calls MyFunction, the error handler in MySub will catch the problem when
it
: calls MyFunction and the problem occurs.

Actually, I don't really understand that. Suppose I have

Sub MySub
Debug.Print MyFunction(0)
End Sub
Function MyFunction(deno m as Integer)
MyFunction = 6/denom
End Function

Why does execution return to MySub so that the error can be trapped
instead of simply dying in MyFunction?
If you have added error handling to MySub, then when it calls a function
that generates an error, control is returned from the failed function to the
calling procedure's error handler.

The example above does not have error handling, so that won't happen until
you add the error handler to MySub.
: While that's true, I prefer to add error handling to MyFunction anyway.
The
: main reason is that I find it much easier to track down an error if I
know
: what procedure caused it. Particularly when you get one proc that calls
: another that calls another ..., I'm too lazy to try to track down which
one
: actually caused the error, when I can get Access to gell me just by
using
: error handling in each one. The other reason is that later on I may need
to
: call MyFunction() from somewhere else, so I really prefer just to have
the
: error handlers in place anyway.

I intend to write a single function that gets offending
sub/function name, etc, as parameters, to report error messages
to the end-user, and after reading your earlier post, I think
that I'll use it to save those messages for the
developer/maintainer as well. Do you just add a table to the
database for these?


Yes, that's what I do. The setup of the table is explained in the article:
http://allenbrowne.com/ser-23a.html

Some developers prefer to write the errors out to a text file, so that even
if the database corrupts you still have the log. IME, that's not a big
enough issue to be worth the effort. The bigger question is whether you use
a single network log for all workstations, or log separately on each
workstation. I prefer logging to a local workstation, because that still
works reliably even if the problems are caused by the network itself.

HTH
Nov 13 '05 #8
Allen Browne wrote:
Some developers prefer to write the errors out to a text file, so
that even if the database corrupts you still have the log. IME,
that's not a big enough issue to be worth the effort. The bigger
question is whether you use a single network log for all
workstations, or log separately on each workstation. I prefer logging
to a local workstation, because that still works reliably even if the
problems are caused by the network itself.


Agreed. My main corporate app logs errors locally, sends me an Email of every
trapped error (there is a table entry that enables/disables that), and also
provides a menu item that allows them to Email me the entire error log file.

It was amazing how many "incidental " errors were happening and not being
reported that I was able to clear up when I added the automatic Email feature.

--
I don't check the Email account attached
to this message. Send instead to...
RBrandt at Hunter dot com
Nov 13 '05 #9
Thanks, Rick.

I do provide an option for the user to export/email/fax a report based on
the error table, but have not given them the automatic email option.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Rick Brandt" <ri*********@ho tmail.com> wrote in message
news:MU******** ********@newssv r11.news.prodig y.com...
Allen Browne wrote:
Some developers prefer to write the errors out to a text file, so
that even if the database corrupts you still have the log. IME,
that's not a big enough issue to be worth the effort. The bigger
question is whether you use a single network log for all
workstations, or log separately on each workstation. I prefer logging
to a local workstation, because that still works reliably even if the
problems are caused by the network itself.


Agreed. My main corporate app logs errors locally, sends me an Email of
every trapped error (there is a table entry that enables/disables that),
and also provides a menu item that allows them to Email me the entire
error log file.

It was amazing how many "incidental " errors were happening and not being
reported that I was able to clear up when I added the automatic Email
feature.

--
I don't check the Email account attached
to this message. Send instead to...
RBrandt at Hunter dot com

Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3377
by: Pete | last post by:
I'm trying to improve my code so that when I open a recordset object, I can absolutely guarantee it is closed and is set = Nothing. I have read some old threads and they all say to use the functional equivalent of the following code: Public Sub CloseRecordset() On Error GoTo Sub_Error Dim rs As Recordset rs.FindFirst "This Will Error"
3
4619
by: Paul | last post by:
I have an Access 2000 database with a form that is giving me some major headaches. When you open the form, it displays all records and allows editing, but has AllowAdditions set to False so that the user has to use my New Record button. When you click the New Record button, the form presents a new record for editing. My client wants to use the Escape key to cancel changes to new or existing records. On existing records, Access already...
9
10886
by: Robert Wing | last post by:
I support an MS Access application in which errors are trapped using the On Error statement. Just recently, the users of this system have experienced run-time error number 3021 on a random basis. My biggest problem right now is that the error description associated with this error is Application-defined or object-defined error. It has always been my understanding that these error numbers would not be used by Microsoft and were available...
3
6898
by: Nathan Bloomfield | last post by:
Hi there, I am having difficulty with a piece of code which would work wonders for my application if only the error trapping worked properly. Basically, it works as follows: - adds records from rsSource into rsDest - if it finds a key violation then it deletes the current record from rsDest and adds the new record from rsSource. This works perfectly - but only for the first found duplicate record, it brings up the error
3
2041
by: Smriti Dev | last post by:
Hi There, I have the following code and when I try to run it, I get a type mismatch error. I would really appreciate your help with this. Thanks kindly, smriti --- Private Sub cmdOK_Click()
2
3876
by: Captain Nemo | last post by:
I'm still using Office 2000 myself, but some of my clients have Office 2003. I've recently added a piece of code to create an instance of Word, open a document, fill in the blanks and become visible so the document can be printed and/or modified. This all takes place within one form, in which the Word.Application and Word.Document objects are both private form-level variables. Just to be on the safe side I included this piece of code in...
9
2108
by: 47computers | last post by:
Pretty new to PHP, I recently started learning about error trapping. As of right now, I include the following into a page in my website: -------BEGIN PASTE-------- error_reporting(E_ERROR | E_PARSE); set_error_handler("SendErrorReport"); function SendErrorReport($errorNumber, $errorMessage, $errorFile, $errorLine, $vars) {
2
19482
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I will be writing this article intended for those who are in the same level, or maybe lower, of my technical knowledge. I would be using layman's words, or maybe, my own words as how I understand them, hoping, you will understand it the same way that...
0
2897
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I will be writing this article intended for those who are in the same level, or maybe lower, of my technical knowledge. I would be using layman's words, or maybe, my own words as how I understand them, hoping, you will understand it the same way that...
0
9382
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9236
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9154
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9087
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8043
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4502
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4762
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2601
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.