Dealing with Logical Errors

Even if your code compiles and runs without generating an error message, the code isn't necessarily perfect. It can also contain logical errors. Unlike a compile error, which is an error in syntax or a typographical error, a logical error is an error in your thinking (logic). The computer always does exactly what the code tells it to do, even if you tell it to do the wrong thing.

Suppose that you intend to write a line of code to open some form, but you accidentally write the code to close the form. When you run the code, the computer (of course) closes — not opens — the form. The computer would never look at your code and think, "Hmmmm. I bet she meant to open a form here, so I'll do that instead." Computers just don't work that way. The computer always does exactly what the code tells it to do.

Figure 12-8:

Equal number of open and closed parentheses.

Pinpointing logical errors in your code is often difficult mainly because when you run a procedure, everything happens in less time than it takes to blink your eyes. Often it helps to take a look at what's going on behind the scenes while the code is running. The VBA Editor provides a few tools that allow you to see what's going on behind the scenes.

Was this article helpful?

0 0

Post a comment