By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,475 Members | 2,093 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,475 IT Pros & Developers. It's quick & easy.

Using a module to trap errors

hyperpau
Expert 100+
P: 184
Hi guys, I am wondering it this is possible.

Basically, what I do currently is use the Form_Error() event to trap
erros and display customer error messages.

Problem is, I need to do this in every form. Of course, i know this is
essential. But wouldn't there be a way to use a module to call during the Form_Error Event?

Example: Instead of doing:
Expand|Select|Wrap|Line Numbers
  1. Public Sub Form_Error(DataErr as Integer, Response as Integer)
  2. If DataErr = 3022 
  3.      'some message
  4. Else
  5.     'some message
  6. End If
  7. End Sub
I would then create a module with this sub:
Expand|Select|Wrap|Line Numbers
  1. Sub ErrorHandler()
  2. If DataErr = 3022 
  3.      'some message
  4. Else
  5.     'some message
  6. End If
  7. End Sub
Then on the form's error event, do this.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Error(DataErr as Integer, Response as Integer)
  2. Call ErrorHandler
  3. Response = DataErrContinue
  4. End Sub
This works when I tried it, the problem is only the Else part works if I put it on the module. It doesn't seem to recognize the DataErr: 3022

Not to mention, when i use the Err.Number always just gives me 0

Thanks in advanced.
Aug 4 '08 #1
Share this Question
Share on Google+
14 Replies


puppydogbuddy
Expert 100+
P: 1,923
see these links for sample global error handler vs procedural error handler:

http://www.vb123.com/Toolshed/04_acc...orhandling.htm

http://www.allenbrowne.com/ser-23a.html

If you agree that procedural error handling is better,safer and more secure.....but wish there is a way to reduce the work effort involved, see this link for a third party tool costing less than $30.

http://www.aadconsulting.com/errhdoff.html
Aug 5 '08 #2

hyperpau
Expert 100+
P: 184
Thank you puppydogbuddy. But I have seen this links already before I posted.
This does not solve my problem.

Both pages still talks about calling the procedure within EVERY ErrorHandler of every sub in the form module.
The 2nd link doesn't even talk about modules but local error handling.
The 1st link uses a standard module function, however, I would still
need to call the function from the ErrorHandler of every sub.

Expand|Select|Wrap|Line Numbers
  1. On Error GoTo ErrorHandler
  2.  
  3. 'some codes
  4.  
  5. Exit_Error:
  6. Exit Sub
  7.  
  8. ErrorHandler:
  9. Call "function name from the standard module"
  10. Resume Exit_Error
This is the tedious task that I want to solve.
What I want is a standard module and then just call the module
from the Form-Error event. This way, I would not have to copy paste
and call the standard module from every sub.

Again, I have accomplished this already, the only problem is that I cannot
customer the MsgBox per DataErr no bec it only reads the Case Else statement and not the specific Error.

Thanks for trying.







see these links for sample global error handler vs procedural error handler:

http://www.vb123.com/Toolshed/04_acc...orhandling.htm

http://www.allenbrowne.com/ser-23a.html

If you agree that procedural error handling is better,safer and more secure.....but wish there is a way to reduce the work effort involved, see this link for a third party tool costing less than $30.

http://www.aadconsulting.com/errhdoff.html
Aug 5 '08 #3

Expert Mod 2.5K+
P: 2,545
Hi Hyperpau. On the value of the dataerr param not being recognised, you are not passing it to your custom error handler from the form_error sub; is it in scope at the time (i.e. global to your current module)?

The form error event does not trap runtime errors in VBA code (as help entry for this event indicates), so you cannot use it as a general error handler without including an error handler redirect in each procedure, or at least within the module concerned in an appropriate place.

I tested this by using a command button on a form which on click does a deliberate division by 0. The form_error event was not triggered.

When I then added a standard On Error Goto to the procedure concerned and redirected from there to the form_error procedure the division by zero was trapped accordingly.I had no problem passing the err.number to my test form_error procedure in place of the dataerr value, nor in displaying the err.description - but as mentioned the trigger was a redirect to the form_error procedure from a normal error redirect in the code concerned.

Extract from help file on form error event is listed below.

-Stewart

The Error event occurs when a run-time error is produced in Microsoft Access when a form or report has the focus. This includes Microsoft Jet database engine errors, but not run-time errors in Visual Basic.
Aug 5 '08 #4

puppydogbuddy
Expert 100+
P: 1,923
To quote Allen Browne from the second link I gave you:

"Every function or sub should contain error handling. Without it, a user may be left viewing the faulty code in a full version of Access, while a run-time version just crashes."

If you look at the third party tool, it will relieve you from the tediousness of having to manually add error handling to every procedure.
Aug 5 '08 #5

hyperpau
Expert 100+
P: 184
Hi to both of you.

I completely understand what you are tyring to tell me guys.

So it simly means that the answer to my question is 'NO'. ??

Meaning, we cannot use a standard module with a select case statement for each DataErr number and just call it from the Form_Error event of each form?

If it is really yes, then I guess, I would just have to copy paste the select case statements to the Form_Error event of every form, because I do not want to do the On Error GoTo for each sub.

But I feel that there is a way to do this because it already works, I am able to call my Select Case statement procedures from my Standard Module, from the Form_Error event of my Form. The only problem i am encountering now is that it does not pass the DataErr number to my standard module, thus only calling the msgbox in the Case Else statement from my Standard Module.

I wonder what Mary, or Fishval, or missingling would say about this. :)
Aug 6 '08 #6

FishVal
Expert 2.5K+
P: 2,653
Hello, hyperpau.

First and for most lets make an evidence that we are talking about the same. ;)
As far as I've got it you are looking for a method of handling Form_Error event with a common code in a public code module. The issue is not about handling VBA errors in form module. Am I right?

If so, then the most convinient way I see so far is to write a public routine which receives:
  • by value error code (Dataerr argument of Form_Error handler)
  • by reference response (Response argument of Form_Error handler) and form object

Something like the following:

Form module:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Error(Dataerr As Integer, Response As Integer)
  2.     'form specific code to be run before common code runs
  3.     FormErrorHandler Dataerr, Response, Me
  4.     'form specific code to be run after common code runs
  5. End Sub
  6.  
Code module:
Expand|Select|Wrap|Line Numbers
  1. Public Sub FormErrorHandler(ByVal Dataerr As Integer, _
  2.                             ByRef Response As Integer, _
  3.                             ByRef frm As Access.Form)
  4.  
  5.     Select Case Dataerr
  6.         Case FormErrorCode1
  7.             With frm
  8.                 'do some form actions
  9.             End With
  10.         Case FormErrorCode2
  11.             '.....
  12.      End Select
  13.  
  14.     Response = acDataErrContinue 'disable standard Access response
  15.  
  16. End Sub
  17.  
Did you mean something like this? ;)

Best regards,
Valentine
Aug 6 '08 #7

hyperpau
Expert 100+
P: 184
OH MY GOD!!!!

It works like a beauty!!!! I knew it!!!!
I knew there is a way! LOL
FishVal, you are the greatest!!!!
I have been looking for this everywhere and here it is, thanks to you!

Now I don't have to copy paste the Select Case statement to each
Form's Error event. I just call the Public Sub from my Public Module and all my errors are handled. What the!!!! hahha. Thank you FishVal!!
This will make my form modules organized and neat for all my next projects.
You're so great!
Aug 7 '08 #8

FishVal
Expert 2.5K+
P: 2,653
:)

You are welcome.
Good luck and happy coding.
Aug 7 '08 #9

NeoPa
Expert Mod 15k+
P: 31,186
...
I wonder what Mary, or Fishval, or missingling would say about this. :)
Nice work Fish!

But WHY WASN'T I IN THAT LIST??!? (sulks :D)
Aug 10 '08 #10

FishVal
Expert 2.5K+
P: 2,653
Nice work Fish!

But WHY WASN'T I IN THAT LIST??!? (sulks :D)
:D

Are you going to nuke the matrix again, Neo?

Kind regards,
Fish
Aug 11 '08 #11

NeoPa
Expert Mod 15k+
P: 31,186
Some of those powers WOULD be nice :D

...But I'm only NeoPa don't forget.
Aug 11 '08 #12

hyperpau
Expert 100+
P: 184
Nice work Fish!

But WHY WASN'T I IN THAT LIST??!? (sulks :D)
I'm sorry NeoPa, I didn't forget you. I promise!
I was previewing the post and found out I clicked the Submit buttone and didn't know how I can UNsubmit it and edit.

Hope you're not mad. :(
Aug 18 '08 #13

NeoPa
Expert Mod 15k+
P: 31,186
...
Hope you're not mad. :(
Mad? No! I've only ever been slightly insane :D

I hope that wasn't too believable - that I'm quite so egocentric.
Aug 19 '08 #14

hyperpau
Expert 100+
P: 184
For those who may be looking for solution that I have found,

I want to share this article. It may not be very technical or I may have not described it in the right naming convention or whatsoever, but it sure works for me. :)

Custom Error Messages in Access
Aug 19 '08 #15

Post your reply

Sign in to post your reply or Sign up for a free account.