150 likes | 432 Views
Errors and Debugging . Learn how to make errors. Syntax Compile Run-time Semantic. Cn’t type Can’t rememb Can’t decide Can’t think. Intercept Errors Before VBA. Utilities must have error handling Anticipate errors in important components On Error GoTo Resume ExitStageLeft.
E N D
Learn how to make errors • Syntax • Compile • Run-time • Semantic • Cn’t type • Can’t rememb • Can’t decide • Can’t think
Intercept Errors Before VBA • Utilities must have error handling • Anticipate errors in important components • On Error GoTo • Resume ExitStageLeft
On Error, whaaaat? • On Error Goto 0 • Disables the error handler • On Error Resume Next • Ignore the error and continue • On Error Goto Label • Jump to Label: for your own code to handle
Polishing off your Resume • Resume • Suggested only for run-time debugging!! • Resume Next • Just keep on running after error detected, don’t report the error at all. • Resume Label • Best. Go to Label: when error detected
Trap for the “Usual suspects” Here is where your own code can handle the errors • If you know division by zero is possible • If Err.Number = 11 Then yadda yadda • If you know data type can be wrong • If Err.Number = 13 Then yadda yadda • How to know Err.Number? MsgBox Err.Number & ": " & Err.Description Resume Exit_Label
VBA Run-time Error Objects • Trap-able • Err object • Err.Number • Err.Description • Err.Source (MSAccess)
Custom Traps • Prevent user errors by making them yourself • If not IsDate(dteUser) Then _ Err.Raise 32001 • If not IsNumeric(varUser) Then _ Err.Raise 32002 • Err.Raise can promote errors to calling Procs.
Form-level Errors • In Form event “OnError” • Assignment2.mdb • Response arg controls Access err msgs acDataErrContinue (override err msgs) acDataErrDisplay (allow err msgs) • Issue your own err msgs: If ActiveControl.Name = "txtDate" then… If ActiveControl.Name = "cboParty" then…
Compile-time errors • Occur before run-time errors • Find these on purpose • Menu selections: Debug, Compile
ADO Run-time error collection • Trap-able • Can be same error numbers as VBA • When same, error belongs to ADO • Useful in debugging linked tables • Useful in client-server situations • Example: frmErrorHandling • Sub ShowErrors()
When are errors reported? • Sometimes, when they are caused • Sometimes, where they are caused • Error handling routine chained back to callers • “Resume” can be dangerous in chains • Best to trap in individual Procs… unless… Subordinate procs return errors for supervisor Proc
Debugging • Calm demeanor is essential • Take a totally objective view • “What does this statement do?” (right!) • “What should this statement do?” (wrong!) • Cardinal rule: get more information
Techniques • Break points, F8 • Hover • Run to cursor • Watch, QuickWatch • Run and Re-run lines of code
Design-time tools • Bulk commenting • Bookmarking source