Generally, there are three types of errors that you will encounter when you are writing code: syntax errors, logic errors, and run-time errors. Each type of error requires a different means of troubleshooting. Following is a brief description of each type of error.
Syntax Errors
Let’s start with the easiest type of program error to deal with, the syntax error. A syntax error occurs when your code is written improperly. An example would be a misspelled or missing variable or keyword:
Dim strName as
Obviously, an essential piece of code is missing in the statement: the data type. A correct statement would read:
Dim strName as String
The key to eliminating syntax errors is to use the Option Explicit statement in every module. Option Explicit will require that all variables are explicitly declared in your project. Explicitly declaring your variables is important for several reasons. Simple misspellings in a variable name can cause unwanted behavior and data loss. When working with databases, corrupt data can be entered without any error ever being generated.
VBA treats a misspelled variable as just another variable. For example,
lPayment? = 7,224.45
Me.txtBalance.Value = lPaymint
Another important reason to use explicit variable declaration is to optimize your project. By default, undeclared variables use the variant data type. This data type may use significantly more memory than you intend for the variable’s purpose. For example, you may want to use a byte variable in your code. Bytes occupy 1 byte of memory space. If you didn’t explicitly declare the variable, it would be initialized as a variant even though it would only hold 1 byte of data. This means that you’d be using up 16 times the memory you actually need.
Remember, you can also use the VBE to force variable declaration. Simply select Tools | Options and go to the Editor tab. Once you’re there you can turn Require Variable Declaration on. This will insert Option Explicit at the top of each new module; however, it will not update your existing modules. Go back into your project and insert Option Explicit in any modules you have previously created.
Also on the General tab is a Background Compile option. This option allows the computer to compile your project in the background while the computer is idle. Selecting the Background Compile option can slightly improve the run-time speed of your project (Compile On Demand must also be enabled to use this feature).
Logic Errors
Logic errors occur when your code doesn’t respond the way you intended. Perhaps your logic is unsound, or the program may flow in a different manner than you intended. The best way to check these errors is to manually step through your code using Watch windows. This can be especially tricky when using a series of UserForms—make sure you understand the call stack.
Run-Time Errors
You cannot avoid run-time errors. However, you can plan for them. Planning for run-time errors involves developing an error handler. The Developer’s Edition of Office 2000 includes a VBA Error Handler add-in that makes working with your error handler easier. Although, we’ll briefly discuss this add-in toward the end of the chapter, we’ll assume you are working in the normal Office environment and discuss how you can implement error handling without this add-in.
Popularity: 1% [?]