Wrestling Runtime Errors

Some VBA errors are caused by environmental conditions rather than anything that's wrong with the logic of the code or a compile error. As an extreme example, take a look at an environmental condition that could prevent code from executing. Say you drag and drop some icons from a folder to the icon for your floppy drive in My Computer. If no floppy disk is in the drive when you release the mouse button, you create an environmental condition in which no program could complete its task. There's just no way that any program in the world can copy files to an empty floppy disk drive!

In your VBA code, environmental conditions can be much more subtle than the missing floppy disk example. A more common example is a line of code that attempts to move the cursor to a specific control when the form isn't even open. For example, the following line of code attempts to move the cursor (SetFocus) to a control named StateProv on an open form named Customers (Forms![Customers].SetFocus).

Forms!Customers.[StateProv].SetFocus

The preceding line of code executes just fine as long as the form named Customers is open in Form view when the line executes. If the form named Customers is open in Design view when that line executes, a runtime error like the one shown in Figure 12-11 occurs.

Figure 12-11:

Sample runtime error message.

Figure 12-11:

Sample runtime error message.

Was this article helpful?

0 0

Post a comment