473,320 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

CAlling another sub from a sub.

hyperpau
184 Expert 100+
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
Aug 20 '07 #1
14 10306
ADezii
8,834 Expert 8TB
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.
Aug 20 '07 #2
missinglinq
3,532 Expert 2GB
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)>
Aug 20 '07 #3
ADezii
8,834 Expert 8TB
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.
Aug 20 '07 #4
missinglinq
3,532 Expert 2GB
No information is useless; well, almost none!

Linq ;0)>
Aug 20 '07 #5
hyperpau
184 Expert 100+
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. :)
Aug 20 '07 #6
hyperpau
184 Expert 100+
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?
Aug 20 '07 #7
ADezii
8,834 Expert 8TB
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
Aug 20 '07 #8
hyperpau
184 Expert 100+
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
Aug 21 '07 #9
vinci
62
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
Aug 21 '07 #10
MMcCarthy
14,534 Expert Mod 8TB
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.
Aug 21 '07 #11
hyperpau
184 Expert 100+
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?
Aug 22 '07 #12
MMcCarthy
14,534 Expert Mod 8TB
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.
Aug 22 '07 #13
hyperpau
184 Expert 100+
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?
Aug 28 '07 #14
MMcCarthy
14,534 Expert Mod 8TB
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.
Aug 28 '07 #15

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

Similar topics

1
by: Asapi | last post by:
1. Are linkage convention and calling convention referring to the same thing? 2. Does calling convention differ between languages C and C++? 3. How does calling convention differ between...
8
by: Muthu | last post by:
I've read calling conventions to be the order(reverse or forward) in which the parameters are being read & understood by compilers. For ex. the following function. int Add(int p1, int p2, int...
7
by: Klaus Friese | last post by:
Hi, i'm currently working on a plugin for Adobe InDesign and i have some problems with that. I'm not really a c++ guru, maybe somebody here has an idea how to solve this. The plugin is...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
3
by: Mike | last post by:
Timeout Calling Web Service I am calling a .NET 1.1 web service from an aspx page. The web service can take several minutes to complete its tasks before returning a message to the aspx page. ...
2
by: Geler | last post by:
A theoretical question: Sorry if its a beginner question. Here is a quote from the MSDN explaning the C/C++ calling convention.. It demonstrates that the calling function is responsible to clean...
47
by: teju | last post by:
hi, i am trying 2 merge 2 projects into one project.One project is using c language and the other one is using c++ code. both are working very fine independently.But now i need to merge both...
7
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file...
10
by: sulekhasweety | last post by:
Hi, the following is the definition for calling convention ,which I have seen in a text book, can anyone give a more detailed explanation in terms of ANSI - C "the requirements that a...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.