Monitor and Debug Your Code Callahan Chapter 6 The Importance of Error Handling n n Developers must anticipate errors and design and code to handle them Don t leave the user looking at an error message wondering what went wrong and how to fix it 1 Stepping Through Code Line By Line n Breakpoint q q q n Step Into F8 q q n an executable line of code at which VB suspends execution and makes the Module s window the active window execution is suspended before the breakpoint statement is executed breakpoints are session specific they are not saved in the database runs one statement including statements in a called procedure then suspends again you can single step through code by using Step Into repeatedly Step Out Ctrl Shift F8 q q runs all lines of code until it finishes the current procedure and then returns execution to the next executable statement in the preceding procedure of the call tree use to avoid stepping through each line of code in a called procedure Stepping Through Code Line By Line n Step Over Shift F8 q n Go Continue F5 q n runs one statement treating a called procedure as one step w o stepping through each statement in the called procedure resumes regular full speed code execution after it has been suspended Reset q q terminates execution of Visual Basic procedures and clears all public and private variables a form can t receive the focus while you re stepping through code n use Reset to stop executing the procedure and return to the form 2 Monitoring Variables and Other Values n Locals Pane q q q n Data Tip q n displays the values of each variable declared within the current procedure also displays Me when event procedure code from a form report is being executed can see the form report s properties and controls collection point to a variable while code is suspended a ToolTip indicates the variable s current value Techniques for Monitoring Your Code q q use Debug Print to send messages to the Debug window use MsgBox Finding and Fixing Bugs in Your Code n This procedure had 3 syntax errors and 1 logic error Private Sub PostalCode AfterUpdate if New York or Seattle postal code fill in city and state fields Select Case PostalCode Case 10000 To 10200 City New York State NY Case 98100 To 98199 City Seattle State WA If PostalCode 00000 And 99999 Then Country USA Title SetFocus End Select End Sub 3 Types of Errors n Syntax errors q q n Incomplete code q n an error in the grammatical structure of the statement or expression when syntax checking is enabled errors are highlighted after you type a line and press ENTER code statement that isn t complete eg If without Then or End If Invalid References q q q when you misspell a variable name field name or procedure name or if you did not declare a variable you use Access displays an error when the code is called Data Dictionary helps ensure you plan for and name your variables and use them consistently Types of Errors n Logic errors q q q a programming error that can cause code to produce incorrect results or halt execution Access VB only do what you tell them to do generally the toughest to correct n Design Documents help ensure logic is thought out before you write any code q n q n IPO Pseudocode use carefully planned test data and debugging tools to help track down Desk Check Run Time errors Callahan 7 q q q an error that occurs when VBA code is running results when a statement attempts an invalid operation egs n n attempting an illegal operation such as dividing by zero writing data to a file that doesn t exist 4 Telling Access to Check Your Code n Code must be compiled before it can run execute q q n Access checks the entire project for statement syntax object names referenced variables really exist called procedures really exist etc stores the compiled version of the code Access compiles your code q when you tell it to n q n n Debug Compile compiles all standard modules and form report modules on demand if you don t compile your code before running it Access compiles it when called Application IsCompiled property nib q indicates whether the project s code is in a compiled state Telling Access to Check Your Code n Access considers the project as decompiled whenever q q q q n n n change a module s code create or delete a form report containing VBA code rename or delete a control module procedure rename the database Decompiled code runs slower and is a lurking time bomb Before distributing your application use Debug Compile to compile all procedures and save all code in the database in its compiled state Warning the mere fact that your code compiles does not guarantee it is the correct code 5 Select Case n n A clearer alternative to a series of nested If statements Each case is evaluated sequentially q the first true Case is executed n q any remaining cases are ignored processing resumes after End Select Private Sub FilterOptions AfterUpdate This is an embellishment If FilterOptions 3 Then Me Filter City Phoenix Me FilterOn True ElseIf FilterOptions 2 Then Me Filter City New York Me FilterOn True Else Me FilterOn False End If Private Sub FilterOptions AfterUpdate This is an embellishment Select Case FilterOptions Case 3 Me Filter City Phoenix Me FilterOn True Case 2 Me Filter City New York Me FilterOn True Case 1 Me FilterOn False End Select End Sub End Sub 6
View Full Document
Unlocking...