Download presentation
Presentation is loading. Please wait.
Published bySherman Sullivan Modified over 9 years ago
1
Errors and Debugging
2
Learn how to make errors Syntax Compile Run-time Semantic Cn’t type Can’t rememb Can’t decide Can’t think
3
Intercept Errors Before VBA Utilities must have error handling Anticipate errors in important components On Error GoTo Resume ExitStageLeft
4
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
5
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
6
Trap for the “Usual suspects” 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 Here is where your own code can handle the errors
7
VBA Run-time Error Objects Trap-able Err object Err.Number Err.Description Err.Source (MSAccess)
8
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.
9
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…
10
Compile-time errors Occur before run-time errors Find these on purpose Menu selections: Debug, Compile
11
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()
12
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
13
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
14
Techniques Break points, F8 Hover Run to cursor Watch, QuickWatch Run and Re-run lines of code
15
Design-time tools Bulk commenting Bookmarking source
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.