VBA - Error handling with multiple forms -
There are many forms in my application that say to each other, with the error handling I ran into a problem and as posted here To make the project very large, I created the smallest subset of the code that still reproduces the problem.
-
raiseError
- The function that just raises an error - file
mainForm
- a form with a button - click onaddClientForm
< / Li> - < Code> addClientForm - which generates an error
raiseError
- via
showMainForm
- a collar that isMainForm
shows the code is really simple,
module error
Public sub raiseError () Call Err.Raise (2048, "errorSource", "errorDescription") Last sub
MainForm
Personal sub cbAddClientForm_Click () Dim xAddClientForm as new addClientForm: xAddClientForm.Show End Sub
AddClientForm
Private sub UserForm_Initialize () call raiseError end The new mainForm in the form of a public sub-collar () or GoTo ErrorHandler DM xMainForm: call xMainForm.Show
caller ErrorExit: Sub Out Error Handler: Resume Error on Call MsgBox ("Error Appeared", vbOKOnly) Next GoTo Error Exit End Sub
caller
a Mainfile
and once the user clicks on the button, a addClientForm
starts due to which the call is due to raiseError
. The problem is that ErrorHandler
does not hold the caller
error! Instead, the error raised in raiseError
is treated as an uncontrolled error! This means that an ugly messaging box arrives and sends the user to the correct code in the VB editor - this is what I want to save.
I leave around with different setups of this problem and unless the sequence of the form where one calls each other, works as expected to deal with the error in the enclosed file, all the checks Along with the tested hypothesis, it has been written in various modules.
Then there are questions
- Why is this happening?
- How can this be resolved, that is how I can be raised in such a way Are the error such that the user has not been thrown into the VB editor?
Any help or workaround is greatly appreciated. Daniel
I can not say for some why is happening to me It can not make itself very provocative, but there is a bit of a work around. You can declare ClientForm
and hold on the error by custom OnError
event that arises from within itself ClientForm
You can not do that for MainForm
to do this because you can not declare a variable with events
inside a module, it should be a class.
The big downside here is that you need to manually redirect the errors to the OnError
event in the client form, and then there is an event process for each client form. That is what you expect to handle errors from Other fall is
, then everything, you can consider handling any errors locally in the client form.
Module 1
Public sub-collar () New as a xMainForm MainForm xMainForm.Show End sub
MainForm
Set as a Personal Personality Exchanges Clientform Private sub cbAddClientForm_Click () xClientForm = New ClientForm xClientForm.Show End Sub Private Sub XClientForm_OnError (error as ErrObject) MsgBox Err.Number & amp; ":" & Amp; Err.Description & amp; VbNewLine & amp; Err.Source & amp; "Error raised.", VbCritical, "Error" End Sub
ClientForm
Option Clear Public Event OnError (Error ErrObject As Private Sub UserForm_Click () error on GoTo ErrHandler: Exit raiseError sub ErrHandler: RaiseEvent OnError (Err) End Sub Private Sub UserForm_Initialize () can not increment incidents from 'initialize, they do not hear' 'https' : //stackoverflow.com/q/26589039/3198973 & all folk sub raiseError () Err.Raise 2048, TypeName (Me), "Error description" ending sub
Comments
Post a Comment