Debugging VBA Procedures and Handling Errors

In the course of writing or editing VBA procedures, no matter how careful you are, you're likely to make some mistakes. For example, you may misspell a word, misplace a comma or quotation mark, or forget a period or ending parenthesis. These kinds of mistakes are known as syntax errors. Fortunately, Visual Basic is quite helpful in spotting these kinds of errors. To have Visual Basic automatically check for correct syntax after you enter a line of code, choose Tools | Options in the Visual Basic window. Make sure the Auto Syntax Check setting is selected on the Editor tab.

Figure 9-1: The Auto Syntax Check setting on the Editor tab of the Options dialog box lets you pick up a lot of typos in your VBA procedures.

When Visual Basic finds a syntax error, it displays an error message box and changes the color of the incorrect line of code to red, or another color as indicated on the Editor Format tab in the Options dialog box.

If the explanation of the error in the error message isn't clear, you can click the Help button for more help. If Visual Basic cannot point you in the right direction, you must return to your procedure and carefully examine the offending instruction for missed letters, quotation marks, periods, colons, equals signs, and beginning and ending parentheses. Finding syntax errors can be aggravating and time consuming. Certain syntax errors can be caught only during the execution of the procedure. While attempting to run your procedure, Visual Basic can find errors that were caused by using invalid arguments or

Introduction to Access 2003 VBA Programming omitting instructions that are used in pairs, such as If.. .End statements and looping structures.

You've probably heard that computer programs are "full of bugs." In programming, errors are called bugs, and debugging is a process of eliminating errors from your programs. Visual Basic provides a myriad of tools with which to track down and eliminate bugs. The first step in debugging a procedure is to correct all syntax errors. In addition to syntax errors, there are two other types of errors: run-time and logic. Run-time errors, which occur while the procedure is running, are often caused by unexpected situations the programmer did not think of while writing the code. For example, the program may be trying to access a drive or a file that does not exist on the user's computer. Or it may be trying to copy a file to a floppy disk without first determining whether the user had inserted a floppy disk and closed the drive door. The third type of error, a logic error, often does not generate a specific error message. Even though the procedure has no flaws in its syntax and runs without errors, it produces incorrect results. Logic errors happen when your procedure simply does not do what you want it to do. Logic errors are usually very difficult to locate. Those that happen intermittently are sometimes so well concealed that you can spend long hours, even days, trying to locate the source of the error.

0 0

Post a comment