CAlling another sub from a sub.  | Expert | | Join Date: Jun 2007
Posts: 177
| |
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: - Private Sub Form_OnError(DataErr As Integer, Response As Integer)
-
-
If DataErr = 3317 Then
-
MsgBox "This is a customized error message"
-
Reponse = DataErrContinue
-
ElseIf DataErr = 3314 Then
-
MsgBox "This is another customized error message"
-
Reponse = DataErrContinue
-
Else
-
MsgBox Err.Description
-
Reponse = DataErrContinue
-
End If
-
-
End Sub
-
-
Private Sub cmdSave_Click()
-
On Error GoTo ErrorHandler
-
-
Docmd.RunCommand acCmdSaveRecord
-
-
ExitSub:
-
ExitSub
-
-
ErrorHandler:
-
'statement to recall the OnError event of the form
-
Response = DataErrContinue
-
Resume ExitSub
-
-
End Sub
|  | Expert | | Join Date: Apr 2006 Location: Philadelphia
Posts: 5,217
| | | 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: - Private Sub Form_OnError(DataErr As Integer, Response As Integer)
-
-
If DataErr = 3317 Then
-
MsgBox "This is a customized error message"
-
Reponse = DataErrContinue
-
ElseIf DataErr = 3314 Then
-
MsgBox "This is another customized error message"
-
Reponse = DataErrContinue
-
Else
-
MsgBox Err.Description
-
Reponse = DataErrContinue
-
End If
-
-
End Sub
-
-
Private Sub cmdSave_Click()
-
On Error GoTo ErrorHandler
-
-
Docmd.RunCommand acCmdSaveRecord
-
-
ExitSub:
-
ExitSub
-
-
ErrorHandler:
-
'statement to recall the OnError event of the form
-
Response = DataErrContinue
-
Resume ExitSub
-
-
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.
|  | Moderator | | Join Date: Nov 2006 Location: Richmond, Virginia USA
Posts: 3,000
| | | 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)> |  | Expert | | Join Date: Apr 2006 Location: Philadelphia
Posts: 5,217
| | | 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.
|  | Moderator | | Join Date: Nov 2006 Location: Richmond, Virginia USA
Posts: 3,000
| | | re: CAlling another sub from a sub.
No information is useless; well, almost none!
Linq ;0)> |  | Expert | | Join Date: Jun 2007
Posts: 177
| | | 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. :)
|  | Expert | | Join Date: Jun 2007
Posts: 177
| | | 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?
|  | Expert | | Join Date: Apr 2006 Location: Philadelphia
Posts: 5,217
| | | 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: - Public Sub HandleAllErrors(lngErrorNumber As Long, strErrorDescription As String, strErrorSource As String)
-
Dim Msg As String
-
Msg = "Error Number: " & lngErrorNumber & vbCrLf & _
-
"Error Description: " & strErrorDescription & vbCrLf & _
-
"Error Source: " & strErrorSource
-
-
Select Case lngErrorNumber
-
Case 1
-
Case 11 'Division by 0
-
MsgBox "Somewhere in your calculations you divided by 0", vbExclamation, "Division by Zerro"
-
Case 76 'Path not Found
-
MsgBox "The Path you are trying to create or access does not exist", vbExclamation, "Path Not Found"
-
Case 234
-
Case 888
-
Case Else 'Unhandled Errors
-
MsgBox Msg, vbExclamation, "Global Error Handler"
-
End Select
To direct Error Codes to the Handler: - Private Sub Command42_Click()
-
On Error GoTo Err_Command42_Click
-
-
'Will cause a Division by 0 Error
-
Dim X
-
X = 123 / 0
-
-
Exit_Command42_Click:
-
Exit Sub
-
-
Err_Command42_Click:
-
Call HandleAllErrors(Err.Number, Err.Description, Err.Source)
-
Resume Exit_Command42_Click
-
End Sub
|  | Expert | | Join Date: Jun 2007
Posts: 177
| | | 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: - Public Sub HandleAllErrors(lngErrorNumber As Long, strErrorDescription As String, strErrorSource As String)
-
Dim Msg As String
-
Msg = "Error Number: " & lngErrorNumber & vbCrLf & _
-
"Error Description: " & strErrorDescription & vbCrLf & _
-
"Error Source: " & strErrorSource
-
-
Select Case lngErrorNumber
-
Case 1
-
Case 11 'Division by 0
-
MsgBox "Somewhere in your calculations you divided by 0", vbExclamation, "Division by Zerro"
-
Case 76 'Path not Found
-
MsgBox "The Path you are trying to create or access does not exist", vbExclamation, "Path Not Found"
-
Case 234
-
Case 888
-
Case Else 'Unhandled Errors
-
MsgBox Msg, vbExclamation, "Global Error Handler"
-
End Select
To direct Error Codes to the Handler: - Private Sub Command42_Click()
-
On Error GoTo Err_Command42_Click
-
-
'Will cause a Division by 0 Error
-
Dim X
-
X = 123 / 0
-
-
Exit_Command42_Click:
-
Exit Sub
-
-
Err_Command42_Click:
-
Call HandleAllErrors(Err.Number, Err.Description, Err.Source)
-
Resume Exit_Command42_Click
-
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
|  | Member | | Join Date: Aug 2006
Posts: 63
| | | 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
|  | Administrator | | Join Date: Aug 2006 Location: Dublin, Ireland
Posts: 10,885
| | | 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.
|  | Expert | | Join Date: Jun 2007
Posts: 177
| | | 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?
|  | Administrator | | Join Date: Aug 2006 Location: Dublin, Ireland
Posts: 10,885
| | | 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.
|  | Expert | | Join Date: Jun 2007
Posts: 177
| | | 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?
|  | Administrator | | Join Date: Aug 2006 Location: Dublin, Ireland
Posts: 10,885
| | | 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.
|  | Similar Microsoft Access / VBA bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,467 network members.
|