-
I want to know how you guys do error trapping...
What if I got two private sub that need the same error trapping routine... I have to code the same trapping code on two subs. Is there a better way like one place can call trapping code in another sub.
Thx
-
Well if you're calling both subs from the same sub, you could put an errorhandling in that sub. Else you have to put the errorhandling in both subs.
-
I believe that you can also put it into a module. Here's an example. Make a Form with a CommandButton and add a module as well.
In the CommandButoon, put the following code. In this example, we declare A as a Byte, which limits the numbers from 0-255. If you go above 0, you'll get an
Error. When we get the Error, we call the Function Trap from our module to handle it.
Code:
Private Sub Command1_Click()
On Error GoTo 100
Dim a As Byte
a = 5983982 ' Set the value above 255 so weget an Error
Exit Sub
100
Call Trap(a)
End Sub
Now, put this code into the module. This is the code that will handle the Error.
Code:
Function Trap(ByVal Num As Integer)
MsgBox ("Error")
End Function
Now, whenever you have an Error in a different Sub, you just
call this Function to handle it.