Microsoft Office Tutorials and References
In Depth Information
Trapping Runtime Errors
Error-handling code by itself doesn’t fix the error or allow the procedure to
continue its job. If an error does occur, you see the message (text) and the
number that identifies that message, so you can add code to your custom
error handler to fix the problem and resume code execution normally.
Suppose that the main body of the code is trying to move the cursor into a
control named Company on the current form, using the statement DoCmd.
GoToControl “Company”. If you run the procedure when the form that
contains the Company field isn’t open, a runtime error occurs. The error
handler displays the message box shown in Figure 6-2. Code execution stops
because nothing in the error handler takes care of the problem.
Figure 6-2:
A sample
by an error
The error description and number (2046) display in the error message. In
this particular example, the GoToControl action isn’t available because the
form that the code expects to be open isn’t open. The solution is to come up
with a means of making sure that the appropriate form is open before the
code executes.
Fixing runtime errors
One way to handle the problem is to use an If...End If block (or Select
Case...End Select block) to provide a solution to error 2046. Because
error 2046 tells you that a form the code expects to be open is in fact closed,
the solution is to open the appropriate form, as in the following example:
[code above handler]
‘Trap and fix error 2046.
If Err.Number = 2046 Then
DoCmd.OpenForm (“Address Book Form”)
Resume ‘Try again now that form is open.
End If
‘Errors other than 2046 still just show info and exit.
Msg = Err.Description & “ - “ & Err.Number
MsgBox Msg
Resume Exit_MyProcedure
End Sub
Search JabSto ::

Custom Search