Microsoft Office Tutorials and References
In Depth Information
Dealing with Logical Errors
Preventing runtime errors
A cleaner, more elegant solution to the problem, though, is to rewrite the
procedure so that the runtime error can’t possibly occur. In the following
example, the procedure starts by checking to see whether the required form
is already open. If it’s not, the procedure opens the form before the DoCmd.
GoToControl statement executes.
Sub myProcedure1()
On Error GoTo Err_myProcedure
‘Make sure Address Book form is open.
If Not isOpen(“Address Book Form”) Then
DoCmd.OpenForm “Address Book Form”, acNormal
End If
‘Now move the cursor to the Company field.
DoCmd.GoToControl “Company”
Exit Sub
Msg = Err.Description & “-” & Err.Number
MsgBox (Msg)
End Sub
The isOpen() function used in the preceding example isn’t built into
Access. See Chapter 4 of this minibook for a description of the isOpen()
Dealing with Logical Errors
When your code is free of compile and runtime errors, Access executes every
statement perfectly — which doesn’t necessarily mean that the code does
exactly what you intended. If you were thinking one thing but wrote code that
does something else, an error in the logic of the code occurs — a logical error.
Chapter 6
Logical errors can be tough to pinpoint because when you run a procedure,
everything happens so fast. You’ll find it helpful to slow things down and
watch what happens while the procedure runs. Several tools in Access can
help with that task.
Watching things happen
You can use the Debug.Print statement anywhere in your code to print the
value of a variable, a constant, or anything else. Because all output from the
Debug.Print statement goes to the Immediate window, those statements
don’t disrupt the normal execution of your procedure.
Search JabSto ::

Custom Search