strive4peaceIf you like my work, please donate so I can keep on sharing.
|Access Basics||Video Tutorials||Learn VBA||ListFiles Database||Random Picker||Error Handling||Reference Database||Whistles & Bells||Help Examples||Crystal's Analyzer||Code Documenter|
What is an error handler?
On Error Goto 0 On Error Resume Next On Error GoTo LineLabel Resume Resume Next Resume LineLabel Debug.Print "My variable = " & variable_name MsgBox "My variable = " & variable_name,,"HERE" Exit Sub Exit Function
An error handler is an emergency plan.
Think about ...
In programming, just as real life, it is usually best to have a plan in place.
What happens when there is no plan?
For one thing, the value of global variables can be lost ... and who knows what else... what happens with panic?
On Error GoTo 0= No Plan
cancel a possible previous plan and announce to Access that you have no plan
how can you remember this?
0 = False
= Cancel any previous plan that might have been in place
Go to false? What? There is no plan ... if something unexpected happens -- panic! Do whatever comes to your mind in the emergency.
On Error Resume Nextsays two things:
— so there is an error handler (instructions for what to do)
On Error GoTo LineLabelacts as a detour sign to tell Access what to do if an error happens.
... then come the statements of your procedure ...
... then the lines at the bottom with the Exit code and the actual error handler code – be sure to replace ProcedureName
'This is my "shell" error handling code:
'~~~~~~~~~~~~~~~~~~~~~~ 'set up Error Handler On Error GoTo Proc_Err '~~~~~~~~~~~~~~~~~~~~~~ ' ... then your statements 'put this at the end of the procedure '~~~~~~~~~~~~~~~~~~~~~~ Proc_Exit: On Error Resume Next 'release object variables if applicable-- ie: ' if Not rs is Nothing then ' rs.close ' set rs = Nothing ' end if ' set db = nothing Exit Sub 'or Exit Function Proc_Err: MsgBox Err.Description, , _ "ERROR " & Err.Number _ & " ProcedureName" Resume Proc_Exit 'if you want to single-step code to find error, CTRL-Break at MsgBox 'then set this to be the next statement Resume End Sub
ProcedureName is the name of your Sub or Function so you can identify what code the problem is in when you see the error
If you get an error:
press F5 to continue execution automatically
While I am developing, I like to make the error handler go to the line that caused the problem so I can see where it is. Resume goes back to the offending line. When code Stops, press F8 to execute one statement at a time.
a line label is a word before a colon
it is not an executable statement
when used like this, the colon allows you to combine multiple statements on a single line
Debugging VBA Code -- easy to understand!
by Chip Pearson
comprehensive error reporting tool and articles:
vbWatchdog - Global Error Handler
by Wayne Phillips
If you have comments or want to tell me how my page helped you, thank you — leave your comment on a thread I am currently posting to OR
Free Book on Access: Access Basics
Free Tutorials: Video Tutorialsfor information on private programming and training, or if you have comments and ideas, I'd love to hear from you
Copyright © 2009-2015 Crystal Long | All rights resorved | Last updated Dec 2015 |