PDA

View Full Version : Vb Error Prompt



Emoncada
08-25-2008, 08:47 AM
Is it possible instead of get a run time error or vb error when something is not working right to get a Msg Box to say "Call Tom"

CreganTur
08-25-2008, 09:09 AM
Is it possible instead of get a run time error or vb error when something is not working right to get a Msg Box to say "Call Tom"
*LOL*:rofl:

Yes, this is very possible.

Somewhere near the beginning of the sub or function where you want this, you will put:
On Error GoTo ErrorHandler
This tells VBA that if any error occurs, then you need to go to the Label "ErrorHandler"

At the end of your sub/function, above the End Sub/function you need to type in "Exit Sub" Between the Exit Sub and the End Sub, you would put:
ErrorHandler:
MsgBox "Call Tom"


Now... this would mean they would get the "Call Tom" message for every single error, every time.

You can be more selective- one way is by referencing the error number. For example, if the user tries to change a protected cell or chart, they would get error # 1004. Or if they try to divide by zero, the would get error 11 So, you could do something like this:

ErrorHandler:
Select Case Err.Number
Case 1004
MsgBox "Stop trying to change protected cells!",vbExclamation,"Error: ID-10-T"
Case 11
MsgBox "You cannot divide by zero!"
Case Else
MsgBox "Call Tom"
End Select

You can make your error handling as simple or complex as you want- it's a wonderful feature of VBA!

Emoncada
08-25-2008, 09:37 AM
Is it possible to use it for just Userform error's?

CreganTur
08-25-2008, 09:57 AM
Is it possible to use it for just Userform error's?

You'll need to set it up for the individual subs and functions within the UserForm. There's no overall UserForm error handler- this is because every sub and function can have its own set of expected errors, depending on what it's doing.

Start with looking at the subs that play a major role in your UserForm, and work down to the "less important" ones. There are some that might not need error handling. It's your call :dunno

Carl A
08-25-2008, 10:05 AM
Get MZTools a free addin it will add error handling for you.

http://www.mztools.com/index.aspx

CreganTur
08-25-2008, 11:07 AM
Get MZTools a free addin it will add error handling for you.
MZTools is a great add in, but by default it only adds in the GoTo, the Label, and a basic MsgBox for error handling.

This will do a great job of showing you where to enter your error handling, and provide some basic building blocks to start with, but if you need error-specific handling, then you'd still need to do that by hand.

Bob Phillips
08-25-2008, 12:57 PM
No, you can create an outline error handler, a template, and then use MZTools to insert it. Here is mine

Const mpProcedure as string = "{PROCEDURE_NAME}"

On Error GoTo {PROCEDURE_NAME}_Error
PushProcedureStack mpProcedure, False|True
{PROCEDURE_NAME} = True

If Not AppInitialised Then Err.Raise mgAppErrorNum

{PROCEDURE_BODY}

PopProcedureStack

{PROCEDURE_NAME}_Exit:
Exit {PROCEDURE_TYPE}

{PROCEDURE_NAME}_Error:
{PROCEDURE_NAME} = False
If AppErrorHandler(mmModule, mpProcedure, False|True) Then
Stop
Resume
Else
Resume {PROCEDURE_NAME}_Exit
End If

CreganTur
08-25-2008, 01:03 PM
No, you can create an outline error handler, a template, and then use MZTools to insert it.

You're exactly right, but I was referring to the default setup of the error handler for MZTools- guess I should have made that clear. But yes, you can setup more advanced Error Handlers using MZTools- I just didn't want the OP to think that MZ's default options would take care of all error handling.