Responding to a runtime error

When your code generates a runtime error and you're given the choices shown in Figure 12-11, you have three or four choices:

1 Continue: If code execution was suspended when the error occurred, clicking the Continue button will resume execution at the next line of code in the procedure. This option is disabled (not available) in most cases.

i End: Clicking this button terminates code execution and takes you back to the Code window without going into step mode.

i Debug: Clicking this button stops code and takes you back to the Code window. The line of code that generated the error is highlighted, and you're in step mode.

i Help: Clicking this button provides brief help with debugging VBA code.

Most often, you just click End to get back to your code normally or click Debug to get back to your code with the faulty line highlighted and in step mode. If the problem is something that you can fix in that particular line, you can just modify the line and try again although it often takes a little more brain power than that to figure out what's really wrong.

For example, say upon inspecting the code that caused the runtime error, you discover that the line above the faulty line opens the Customers form in Design view (acDesign) rather than the normal Form view, as follows:

DoCmd.OpenForm "Customers", acDesign Forms!Customers.[StateProv].SetFocus

Even though the error message was generated by the second line, the real problem is in the first line. The fix is to change acDesign to acNormal in the top line, as follows, so that the second line can do its job of moving the cursor to the StateProv control of that form:

DoCmd.OpenForm "Customers", acNormal Forms!Customers.[StateProv].SetFocus

Figure 12-11:

A sample runtime error message.

After you make the correction, you can just run the entire procedure again to test it out.

If seeing the error is difficult, you can still use step mode to watch what's going on in your code and in Access. For example, in your code, you could set a breakpoint a few lines above the line that's causing the error. Then step through your code one line at a time, pressing the F8 key. After you press F8, you can press Alt+F11 to see what (if anything) happened in Access as a result of that statement's execution. Then press Alt+F11 again to return to VBA, press F8 to execute the next statement, and then press Alt+F11 again to see that statement's effect on Access. Just keep doing that, and eventually you'll discover which statement is really causing the situation that's making the faulty line fail.

Unfortunately, not all runtime errors are the kind of things that you can fix by correcting your existing code. Some runtime errors are caused by peculiar situations in the environment, like the missing floppy disk in the copy-to-floppy example, and there's really no way to write code to fix that error. The best that you can do with those kinds of errors is to trap them and give the user some kind of more friendly feedback and options than the VBA runtime error message box would provide.

0 0

Post a comment