Connecting Tech Pros Worldwide Forums | Help | Site Map

CAlling another sub from a sub.

hyperpau's Avatar
Expert
 
Join Date: Jun 2007
Posts: 177
#1: Aug 20 '07
I have set up customized error messages for a form using the
OnError event of the form's vba coding.

However, when the error is trigerred by a command button, the OnError event is not called and you have to manually code that error on the On Error GoTo statement of the command button.

My question is this, how can you recall the coding you made on the OnError event of the form, into the On Error Goto of the command button.

Example:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_OnError(DataErr As Integer, Response As Integer)
  2.  
  3.      If DataErr = 3317 Then
  4.         MsgBox "This is a customized error message"
  5.         Reponse = DataErrContinue
  6.      ElseIf DataErr = 3314 Then
  7.         MsgBox "This is another customized error message"
  8.         Reponse = DataErrContinue 
  9.      Else
  10.         MsgBox Err.Description
  11.         Reponse = DataErrContinue
  12.      End If
  13.  
  14. End Sub
  15.  
  16. Private Sub cmdSave_Click()
  17. On Error GoTo ErrorHandler
  18.  
  19. Docmd.RunCommand acCmdSaveRecord
  20.  
  21. ExitSub:
  22.      ExitSub
  23.  
  24. ErrorHandler:
  25.     'statement to recall the OnError event of the form    
  26.     Response = DataErrContinue
  27.     Resume ExitSub
  28.  
  29. End Sub

ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,217
#2: Aug 20 '07

re: CAlling another sub from a sub.


Quote:

Originally Posted by hyperpau

I have set up customized error messages for a form using the
OnError event of the form's vba coding.

However, when the error is trigerred by a command button, the OnError event is not called and you have to manually code that error on the On Error GoTo statement of the command button.

My question is this, how can you recall the coding you made on the OnError event of the form, into the On Error Goto of the command button.

Example:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_OnError(DataErr As Integer, Response As Integer)
  2.  
  3.      If DataErr = 3317 Then
  4.         MsgBox "This is a customized error message"
  5.         Reponse = DataErrContinue
  6.      ElseIf DataErr = 3314 Then
  7.         MsgBox "This is another customized error message"
  8.         Reponse = DataErrContinue 
  9.      Else
  10.         MsgBox Err.Description
  11.         Reponse = DataErrContinue
  12.      End If
  13.  
  14. End Sub
  15.  
  16. Private Sub cmdSave_Click()
  17. On Error GoTo ErrorHandler
  18.  
  19. Docmd.RunCommand acCmdSaveRecord
  20.  
  21. ExitSub:
  22.      ExitSub
  23.  
  24. ErrorHandler:
  25.     'statement to recall the OnError event of the form    
  26.     Response = DataErrContinue
  27.     Resume ExitSub
  28.  
  29. End Sub

To Call another Sub-Routine, say the Click() Event of the Command6 Button, you would use the syntax Call Command6_Click. To the best of my knowledge, you cannot call a built-in Routine in this same fashion. Since the Form's Error() Event was never triggered, you can't expect to fire it from a non Form related Error. The Arguments DataErr and Response would also have no meaning in that context. What you can do is have a Global Error Handling Procedure which has all Errors passed to it as Arguments. All Error Handling logic for the Application is handled here, and the appropriate actions taken depending on the specific Errors. Hope this helps.
missinglinq's Avatar
Moderator
 
Join Date: Nov 2006
Location: Richmond, Virginia USA
Posts: 3,000
#3: Aug 20 '07

re: CAlling another sub from a sub.


Assuming this is VBA and not VB6, one problem I see here is the very name of your sub!

Private Sub Form_OnError(DataErr As Integer, Response As Integer)

While it's called the OnError event in the form's Property Box, the sub it actually generates is

Private Sub Form_Error(DataErr As Integer, Response As Integer)

Unless this is something that's changed with the latest verison of Access, and I wouldn't think it is (but with Micro$oft you never know) maybe this is why the form level error handling isn't working.

Linq ;0)>
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,217
#4: Aug 20 '07

re: CAlling another sub from a sub.


Quote:

Originally Posted by missinglinq

Assuming this is VBA and not VB6, one problem I see here is the very name of your sub!

Private Sub Form_OnError(DataErr As Integer, Response As Integer)

While it's called the OnError event in the form's Property Box, the sub it actually generates is

Private Sub Form_Error(DataErr As Integer, Response As Integer)

Unless this is something that's changed with the latest verison of Access, and I wouldn't think it is (but with Micro$oft you never know) maybe this is why the form level error handling isn't working.

Linq ;0)>

Hello ling:
Since the Error is being triggered from the Click() Event of a Command Button, the Form's Error() Event will never fire. The Error() Event will fire for Microsoft Jet Database Engine errors, but not run-time errors in Visual Basic or errors from ADO. Just some useless information.
missinglinq's Avatar
Moderator
 
Join Date: Nov 2006
Location: Richmond, Virginia USA
Posts: 3,000
#5: Aug 20 '07

re: CAlling another sub from a sub.


No information is useless; well, almost none!

Linq ;0)>
hyperpau's Avatar
Expert
 
Join Date: Jun 2007
Posts: 177
#6: Aug 20 '07

re: CAlling another sub from a sub.


Quote:

Originally Posted by missinglinq

Assuming this is VBA and not VB6, one problem I see here is the very name of your sub!

Private Sub Form_OnError(DataErr As Integer, Response As Integer)

While it's called the OnError event in the form's Property Box, the sub it actually generates is

Private Sub Form_Error(DataErr As Integer, Response As Integer)

Unless this is something that's changed with the latest verison of Access, and I wouldn't think it is (but with Micro$oft you never know) maybe this is why the form level error handling isn't working.

Linq ;0)>

oh yes, sorry. i did mean the FOmr_Error. :)
hyperpau's Avatar
Expert
 
Join Date: Jun 2007
Posts: 177
#7: Aug 20 '07

re: CAlling another sub from a sub.


Quote:

Originally Posted by ADezii

To Call another Sub-Routine, say the Click() Event of the Command6 Button, you would use the syntax Call Command6_Click. To the best of my knowledge, you cannot call a built-in Routine in this same fashion. Since the Form's Error() Event was never triggered, you can't expect to fire it from a non Form related Error. The Arguments DataErr and Response would also have no meaning in that context. What you can do is have a Global Error Handling Procedure which has all Errors passed to it as Arguments. All Error Handling logic for the Application is handled here, and the appropriate actions taken depending on the specific Errors. Hope this helps.

can you tell me how to make a global error handling procedure?
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,217
#8: Aug 21 '07

re: CAlling another sub from a sub.


Quote:

Originally Posted by hyperpau

can you tell me how to make a global error handling procedure?

In its simplest case, a Global Error Handler is nothing more than a Public Function or Sub-Routine which accepts certain Error Arguments passed to it from Local Procedures. In some cases, and with specific Errors, the probelms actually causing the Errors can be fixed in the Global Handler. Below, please find a simple Global Error Handler:
Expand|Select|Wrap|Line Numbers
  1. Public Sub HandleAllErrors(lngErrorNumber As Long, strErrorDescription As String, strErrorSource As String)
  2. Dim Msg As String
  3. Msg = "Error Number: " & lngErrorNumber & vbCrLf & _
  4.       "Error Description: " & strErrorDescription & vbCrLf & _
  5.       "Error Source: " & strErrorSource
  6.  
  7. Select Case lngErrorNumber
  8.   Case 1
  9.   Case 11       'Division by 0
  10.     MsgBox "Somewhere in your calculations you divided by 0", vbExclamation, "Division by Zerro"
  11.   Case 76       'Path not Found
  12.     MsgBox "The Path you are trying to create or access does not exist", vbExclamation, "Path Not Found"
  13.   Case 234
  14.   Case 888
  15.   Case Else     'Unhandled Errors
  16.     MsgBox Msg, vbExclamation, "Global Error Handler"
  17. End Select
To direct Error Codes to the Handler:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command42_Click()
  2. On Error GoTo Err_Command42_Click
  3.  
  4. 'Will cause a Division by 0 Error
  5. Dim X
  6. X = 123 / 0
  7.  
  8. Exit_Command42_Click:
  9.     Exit Sub
  10.  
  11. Err_Command42_Click:
  12.     Call HandleAllErrors(Err.Number, Err.Description, Err.Source)
  13.       Resume Exit_Command42_Click
  14. End Sub
hyperpau's Avatar
Expert
 
Join Date: Jun 2007
Posts: 177
#9: Aug 21 '07

re: CAlling another sub from a sub.


Quote:

Originally Posted by ADezii

In its simplest case, a Global Error Handler is nothing more than a Public Function or Sub-Routine which accepts certain Error Arguments passed to it from Local Procedures. In some cases, and with specific Errors, the probelms actually causing the Errors can be fixed in the Global Handler. Below, please find a simple Global Error Handler:

Expand|Select|Wrap|Line Numbers
  1. Public Sub HandleAllErrors(lngErrorNumber As Long, strErrorDescription As String, strErrorSource As String)
  2. Dim Msg As String
  3. Msg = "Error Number: " & lngErrorNumber & vbCrLf & _
  4.       "Error Description: " & strErrorDescription & vbCrLf & _
  5.       "Error Source: " & strErrorSource
  6.  
  7. Select Case lngErrorNumber
  8.   Case 1
  9.   Case 11       'Division by 0
  10.     MsgBox "Somewhere in your calculations you divided by 0", vbExclamation, "Division by Zerro"
  11.   Case 76       'Path not Found
  12.     MsgBox "The Path you are trying to create or access does not exist", vbExclamation, "Path Not Found"
  13.   Case 234
  14.   Case 888
  15.   Case Else     'Unhandled Errors
  16.     MsgBox Msg, vbExclamation, "Global Error Handler"
  17. End Select
To direct Error Codes to the Handler:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command42_Click()
  2. On Error GoTo Err_Command42_Click
  3.  
  4. 'Will cause a Division by 0 Error
  5. Dim X
  6. X = 123 / 0
  7.  
  8. Exit_Command42_Click:
  9.     Exit Sub
  10.  
  11. Err_Command42_Click:
  12.     Call HandleAllErrors(Err.Number, Err.Description, Err.Source)
  13.       Resume Exit_Command42_Click
  14. End Sub

wow!!! that's cool. makes sense to me.

one more thing, where do i save this Public sub?
do i save this in the forms vba?
what If i want to be able to call this from the other forms as well?
htnks in advance
vinci's Avatar
Member
 
Join Date: Aug 2006
Posts: 63
#10: Aug 21 '07

re: CAlling another sub from a sub.


Quote:

Originally Posted by hyperpau

wow!!! that's cool. makes sense to me.

one more thing, where do i save this Public sub?
do i save this in the forms vba?
what If i want to be able to call this from the other forms as well?
htnks in advance


hi there! u can save the sub anywhere u like, wether in a class or in a form...

u call it like this: lets say u are in form2 then the sub is in form1:

form1.errorHandler([parameters])

hope this helps....

enjoy coding
msquared's Avatar
Administrator
 
Join Date: Aug 2006
Location: Dublin, Ireland
Posts: 10,885
#11: Aug 21 '07

re: CAlling another sub from a sub.


Quote:

Originally Posted by hyperpau

wow!!! that's cool. makes sense to me.

one more thing, where do i save this Public sub?
do i save this in the forms vba?
what If i want to be able to call this from the other forms as well?
htnks in advance

As a global error handler it should be in a separate module.
hyperpau's Avatar
Expert
 
Join Date: Jun 2007
Posts: 177
#12: Aug 22 '07

re: CAlling another sub from a sub.


Quote:

Originally Posted by mmccarthy

As a global error handler it should be in a separate module.

I tried saving it in a class module but i get an error:

"Compile Error:

Expected Variable or procedure, not module"


how do i do this?
msquared's Avatar
Administrator
 
Join Date: Aug 2006
Location: Dublin, Ireland
Posts: 10,885
#13: Aug 22 '07

re: CAlling another sub from a sub.


Quote:

Originally Posted by hyperpau

I tried saving it in a class module but i get an error:

"Compile Error:

Expected Variable or procedure, not module"


how do i do this?

Not a class module just an ordinary module.
hyperpau's Avatar
Expert
 
Join Date: Jun 2007
Posts: 177
#14: Aug 28 '07

re: CAlling another sub from a sub.


Quote:

Originally Posted by mmccarthy

Not a class module just an ordinary module.

thanks mary. i'll try that.
is this regular module the one created when i click Insert -> Module?


isn't this module going to be in the forms vba only?
how can i use it in other forms' vba?
msquared's Avatar
Administrator
 
Join Date: Aug 2006
Location: Dublin, Ireland
Posts: 10,885
#15: Aug 28 '07

re: CAlling another sub from a sub.


Quote:

Originally Posted by hyperpau

thanks mary. i'll try that.
is this regular module the one created when i click Insert -> Module?


isn't this module going to be in the forms vba only?
how can i use it in other forms' vba?

If you use it as Insert - Module, then its independent of any one form and can be called by all forms.
Reply