Errors and Debugging. Learn how to make errors Syntax Compile Run-time Semantic Cn’t type Can’t...

15
Errors and Debugging

Transcript of Errors and Debugging. Learn how to make errors Syntax Compile Run-time Semantic Cn’t type Can’t...

Page 1: 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.

Errors and Debugging

Page 2: 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.

Learn how to make errors

• Syntax

• Compile

• Run-time

• Semantic

Cn’t type

Can’t rememb

Can’t decide

Can’t think

Page 3: 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

Page 4: 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.

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

Page 5: 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.

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

Page 6: 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.

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

Page 7: 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.

VBA Run-time Error Objects

• Trap-able

• Err object

• Err.Number

• Err.Description

• Err.Source (MSAccess)

Page 8: 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.

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.

Page 9: 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.

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…

Page 10: 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.

Compile-time errors

• Occur before run-time errors

• Find these on purpose

• Menu selections: Debug, Compile

Page 11: 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.

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()

Page 12: 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.

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

Page 13: 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.

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

Page 14: 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.

Techniques

• Break points, F8

• Hover

• Run to cursor

• Watch, QuickWatch

• Run and Re-run lines of code

Page 15: 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.

Design-time tools

• Bulk commenting

• Bookmarking source