VBA Access Errohandler In Modules When Calling Other Modules

Let's say I have multiple modules for my code Module_1, Module_2, and Module_3.

**THIS IS JUST QUICK IN A HURRY SAMPLE BUT GIVES THE IDEA**
If in each Module at the beginning it say If error Goto Errorhandler:
Public Sub Module_1 ()
On error Goto Errorhandler
 Call Module_2
Exit Sub
Errorhandler:
 Yadda Yadda
Exit Sub

Open in new window


Public Sub Module_2 ()
On error Goto Errorhandler
If "" & Value1 = "" Then
 Call Module_3
End If
Exit Sub
Errorhandler:
 Yadda Yadda
Exit Sub

Open in new window


Public Sub Module_3 ()
On error Goto Errorhandler
 'Do Something Here
If Me.Dirty Then
Else
Exit Sub
Errorhandler:
 Yadda Yadda
Exit Sub

Open in new window


Then if there is an error in Module_1 or Module_2....
Which Module will the error handler come from?

This is just a little confusing and I am trouble shooting some modules.
Thanks for the help.
LVL 1
Dustin StanleyEntrepreneurAsked:
Who is Participating?
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
Not quite sure what you are asking ... but since each Function/Sub (You are calling them 'Modules' which is not technically correct) has it's own Error Handler, if an error occurs, it will be handled in that Sub.  Noting that for example in the first Sub ... once it calls Module_2 ... and an error occurs in Module_2  ... the error handler in Module_2 will handle the error ... and so on.

Let me suggest this code mode for each 'Module'

Public Sub Module_1 ()
On error Goto Errorhandler
 Call Module_2
Module_1_Exit:
Exit Sub
Errorhandler:
 Yadda Yadda
Resume Module_1_Exit


Side note:
Modules contain Function and/or Subs.  Modules themselves do not execute. Only Functions and Subs execute.
0
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
I believe Joe has you sorted out, but one thing that most professional devs do is to include the Module name in your error handler:

Public Sub Module_1 ()
On error Goto Errorhandler
 Call Module_2
Exit Sub
Errorhandler:
 Msgbox "Error in YourModuleName.YourSubOrFunctionName: " & err.Description
Exit Sub
0
 
Anders Ebro (Microsoft MVP)Microsoft DeveloperCommented:
First of, to make sure we use the right names for objects, a module is a container for several procedures(sub) and functions. The important thing here is SCOPE. A procedure in Module A, can call any function or procedure within module A, even if that procedure is listed as private. Code in module B can only call procedures in module A, if those procedures are listed as public.

So if we have PROCEDURE (proc for short)  1 call proc 2, call proc 3, and an error occurs, then what will happen is: If proc 3 has an error handler, that error handler will take effect. If proc 3 does not have an error handler, the error handler of proc 2 will take effect. If proc 2 also does not have an error handler, the handler in proc 1 will take effect. If neither has an error handler, than the access default will take effect, and you will get the msgbox with the debug option, which you have probably seen before.

Sometimes this can be quite usefull. Say Proc 1 calls proc 2 a hundred times. If proc 2 has an error handler, giving a msgbox, then resumes, you might get that error message 100 times. So sometimes it preferable to only have error handlers in the high level procedures, such as Proc 1. That way only 1 error message is presented to the user.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
Fabrice LambertFabrice LambertCommented:
In general case, when an error happen:

If the function or procedure have an error handler, execution control will be given to it. Otherwise, the error will escalate to the calling function or procedure.
If there are no more calling function or procedure, and the error isn't handled, the execution will stop (collapsing the VBA project in the process) and a system message box will show up for the user.
0
 
Dustin StanleyEntrepreneurAuthor Commented:
Sorry for the wrong wording. Honestly I knew this but don't know why I worded it that way.  I wasn't 100% sure but pretty sure. This now confirms it for me.  Generally I do add the Module name to the Error Handler Like So:

Exit Function
ErrorHandler:
 MsgBox "Error " & Err.Number & ": " & Err.Description & " in " & _
   VBE.ActiveCodePane.CodeModule, vbCritical, "Error in  MODULE NAME HERE"
End Function

Open in new window

0
 
Dustin StanleyEntrepreneurAuthor Commented:
Thank you all for the help. This will help me try to figure out what my exact problem is.
1
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.