Next Tip  An Access Global Error Handler

By Rob Henderson   Find an alternative approach here

Most VBA programmers tend to stick with the local flavour of error reporting within their applications. By using a global error handler you can achieve the following;

1. You can introduce new parameters for any error trapped. I.e. Where was the error called from frmMain, frmContact, etc. This can be particularly helpful when debugging errors in multiple function chain calls.

2. You can introduce database logging once in the function, whereas your only other option is to include in any error trap locally.

3. The learning curve of your applications is slighter for any other developer. I.e. All the error code is in one place.

4. Your users will see the same formatted message type each time your application encounters a problem.

5. You can include a point of contact such as an email address.

To use this technique, place the following function in a new module called mdlError.

Function ErrorLog(strErrDesc As String, lngErrNum As Long, strWhereFrom As String) As Boolean

On Error GoTo Err_ErrorLog

Dim strMsg As String


ErrorLog = True

strMsg = strMsg & "There has been an error in the application." & vbCrLf & vbCrLf
strMsg = strMsg & "Error Number: " & lngErrNum & vbCrLf
strMsg = strMsg & "Error Description: " & strErrDesc & vbCrLf
strMsg = strMsg & "Error Location: " & strWhereFrom & vbCrLf

strMsg = strMsg & "Please note the above information when contacting Support." & vbCrLf & vbCrLf
strMsg = strMsg & "support@developersforhire.co.uk"

MsgBox strMsg, vbInformation, "Error Log."


Exit_ErrorLog:
Exit Function

Err_ErrorLog:

ErrorLog = False

MsgBox Err.Description, vbExcalmation, "Error detected."
Resume Exit_ErrorLog

End Function

Use the following code to call the ErrorLog…

Within a sub or function, place the call to error log where you would normally place your error code. I.e.
Err_SomeRoutine: 
Call ErrorLog(Err.Description, Err.Number, Me.Name) 
NOT

Err_SomeRoutine: 
MsgBox Err.Description, Err.Number, "Error" 

The last argument is where the error code is sitting. In the above example we are passing the name of the form we are currently executing the code from.

You could improve the functionality of the above function by introducing logging to a database table. We have included this functionality within the download

Rob Henderson
Developer from Aberdeen in Scotland

  Your Sample Database Is Called   "global_err_handler.zip"

Sample database is suited to all versions of Access

If you do NOT own "The Toolbox", Click here to find out how to purchase The Toolbox.

 

Other Pages On This Site You Might Like To Read

Track all changes made to a record in Microsoft Access
Access 2002 (XP) Trappable Errors Numbers And Descriptions
Replace Your File API’s With The FileDialog Object

Click on the following button Next Tip to jump to the next page in the document loop.

 

Our Tools and Resources

  • RSS & Newsletter Here
    Join our newsfeed or sign up for our informative newsletter on Office Automation, Access and VB topics

  • The Workbench
    Find out who has your database open, start the correct version of Access, easy compacting and backups, change startup options, creation versions,  shutdown database

  • Read about the Toolbox
    Sample downloads, library resource kit and searchable help file comprising most of the information at vb123.com.au plus hidden downloads etc.

  • Convert Access to SQL Server  
    Upsize to SQL Server 2005 or 2008, easily repeated conversions, highly accurate SQL query  translation and web form conversion.

  • Datamining & Graphs in Access
    Explore your data with this versatile graphing and data mining shareware tool.

  • Expression/SharePoint Web Conversions  
    FrontPage to Expression Web or SharePoint Designer, its a good way to improve your website

 

vb123 Professionals


Get Good Help Here

If you need help with a database or Office programming, our Professionals could be the answer because we have worked on many similar solutions



Frontpage Conversions
We have converted vb123.com to Expression Web, contact us if we can help you move to the latest Microsoft web tool.


About The Editor ~ Contact Us
Garry Robinson writes for a number of popular computer magazines, is now a book author and has worked on 100+ Access databases. He is based in Sydney, Australia

Commercial
Global Error Handler


"For a completely innovative way of achieving what Rob talks about in this article (and much more) consider using Wayne's SimplyVBA Global Error Handler.  

Wayne's tool gives you a way of achieving this without having to alter every single subroutine to implement the global function which in turn reduces code size considerably.  Free developer edition for non commercial use is also available."

Read More here