100 likes | 201 Views
Error Handling & other very practical uses for VBA in MS Access 2000 Part II. Error Categories. Syntax Errors Leaving a line of code Line of code changes color When VBA compiles code or module Runtime Errors (Synchronous & Asynchronous) Invoking an Error Handler Reacting to the Error
E N D
Error Handling& other very practical uses for VBA in MS Access 2000Part II
Error Categories • Syntax Errors • Leaving a line of code • Line of code changes color • When VBA compiles code or module • Runtime Errors(Synchronous & Asynchronous) • Invoking an Error Handler • Reacting to the Error • Exiting the Error Handler • Logic Errors • Improper logic • Code implications • Mistakes Error Handling & VBA - Part II
Syntax Errors VBE | Tools | Options Immediately checks after line of code is entered Forces variable declaration with Option Explicit Info that completes code at insertion point Info about parameters allowed for functions Displays values over cursor only in break mode Error Handling & VBA - Part II
Runtime Errors • On Error GoTo Error_Handler • Gives the most control for error handling • Causes VBA to jump to that location • On Error Resume Next • Allows for VBA to ignore error & continue • On Error GoTo 0 • Cancels the error handler • Resets the value of the Err Object Error Handling & VBA - Part II
Logic Errors • Bugs! Me ? • Adhere to naming convention • Comment your code as much as possible • Require variable declaration • Fix logic errors as they appear • Use the tightest possible scope on variables • Avoid variant data type if possible • Organize, indent, group & label your code • Use sub-procedures and functions • Make one change at a time Error Handling & VBA - Part II
Debugging Tools • Breakpoints and single step mode • Set locations where VBA temporarily halts • Single step one line at a time (F8) • Immediate window(CTRL G) • Investigate VBA effects directly • Always displays the last 200 lines of output • Data Tips work like Tool tips • Quick watches on variables(SHIFT F9) • MsgBox function Error Handling & VBA - Part II
The Err Object Error Handling & VBA - Part II
Error Handling Error Handling Flow Diagram Error Handling & VBA - Part II
Conclusion • Errors Happen - Handle them “elegantly” • Error must be reproducible every time • Structure and comment your code • Use naming conventions • Organize, indent, group & label your code • Modularize your code • Use few or no global nor public variables • Make one change at a time and test Error Handling & VBA - Part II
Questions Error Handling & VBA - Part II