Microsoft Access VBA/VB6 Debugging Tips and Techniques: Basic Error Handling

Provided by: Luke Chung, FMS President

Professional applications need to include error handling to trap unexpected errors. By using a consistent error handler, you can make sure that when crashes occur, the user is properly informed and your program exits gracefully. Basic error handling just hides the default behavior and exits the program. Advanced error handling can include all sorts of features such as saving information about the cause of the error and the environment at the time, attempts to address the problem, and information for the user on what they need to do next.

Verify Error Trapping Setting

Before you can use error handling, you need to understand the Error Trapping setting. VB6/VBA lets you to determine how it should behave when errors are encountered. From the module editor (IDE), look under the Tools Options setting.

Error Trapping Options

Make sure error trapping is not set to "Break On All Errors". That setting will cause your code to stop on every error, even errors you are properly handling with "On Error Resume Next".

"Break on Unhandled Errors" works in most cases but is problematic while debugging class modules. During development, if Error Trapping is set to "Break on Unhandled Errors" and an error occurs in a class module, the debugger stops on the line calling the class rather than the offending line in the class. This makes finding and fixing the problem a real pain.

We recommend using "Break in Class Modules" which stops on the actual crashing line. However, be aware that this does not work if you use raise errors in your classes via the Err.Raise command. This command actually causes an "error" and makes your program stop if Error Trapping is set to "Break in Class Modules".

Unfortunately, users can modify this setting before launching your application so you should make sure this is properly set when your application starts.

Programmatically, the option settings can be viewed and modified using the Application.GetOption and Application.SetOption methods.

Function GetErrorTrappingOption() As String
  Dim strSetting As String
  Select Case Application.GetOption("Error Trapping")
    Case 0
      strSetting = "Break on All Errors"
    Case 1
      strSetting = "Break in Class Modules"
    Case 2
      strSetting = "Break on Unhandled Errors"
  End Select
  GetErrorTrappingOption = strSetting
End Function

Here's a procedure that helps you change the setting:

Sub SwitchErrorHandling(fInit As Boolean)
  ' Copyright (c) FMS, Inc.
  ' Comments: Set or reset error handling routines
  '           This should be called when the program starts and ends.
  '           Avoids problems if the user has Break On All Errors which causes 
  '           On Error Resume Next code to fail.
  ' Params  : fInit TRUE for setting option, FALSE for resetting to original value
  Const cstrOptionErrorTrapping As String = "Error Trapping"
  Const cintBreakInClassModules As Integer = 1
  Static intErrorTrapping As Integer
  Static strSet As String
  If fInit Then
    strSet = cstrOptionErrorTrapping
    intErrorTrapping = Application.GetOption(cstrOptionErrorTrapping)
    Application.SetOption cstrOptionErrorTrapping, cintBreakInClassModules
    ' Do not reset error trapping if the static variable has been reset.
    ' This prevents an accidental reset to breaking on all errors
    If strSet = cstrOptionErrorTrapping Then
      Application.SetOption cstrOptionErrorTrapping, intErrorTrapping
    End If
  End If
End Sub

Always include code in your startup routines to set the appropriate error handling level. At the beginning of your application, call:

SwitchErrorHandling True

When your program finishes, reset this back to the original setting with:

SwitchErrorHandling False

Table Design

Query Design

Form Design

Form Tips and Mistakes

Copy Command Button and Keep Picture

Module VBA to Forms and Controls

Form Navigation Caption

Resync Record in a Subform

Synchronize Two Subforms

Multiple OpenArgs Values

Late Bind Tab Subforms

Subform Reference to Control Rather than Field

Tab Page Reference

Shortcut Keys

Combo Box Top 6 Tips

Properties and Validation

Select First Item

Cascading Combo Boxes

Zip, City, State AutoFill

Report Design

Suppress Page Headers and Footers on the First Page of Your Report

Add the NoData Event

Annual Monthly Crosstab Columns

Design Environment

Add Buttons to the Quick Access Toolbar

Collapse the Office Ribbon for more space

VBA Programming

Basics: Forms and Controls

Run VBA Code from a Macro

Use Nz() to Handle Nulls

Avoid Exits in the Body of a Procedure

Shortcut Debugging Keys

Set Module Options

Math Rounding Issues

Rename a File or Folder

Avoid DoEvents in Loops

Age Calculations

Weekday Math

Send Emails with DoCmd.SendObject

Source Code Library

Microsoft Access Modules Library

Microsoft Access Modules

VBA Error Handling

Error Handling and Debugging Techniques

Error Number and Description Reference

Basic Error Handling

Pinpointing the Error Line

Performance Tips

Linked Database

Subdatasheet Name

Visual SourceSafe


Runtime Downloads

Simulate Runtime

Prevent Close Box

Disable Design Changes

Broken References

Remote Desktop Connection Setup

Terminal Services and RemoteApp Deployment

Reboot Remote Desktop

Missing Package & Deployment Wizard

Avoid Program Files Folder

Unavailable Mapped Drives

Microsoft Access Front-End Deployment

System Admin

Disaster Recovery Plan

Compact Database

Compact on Close

Database Corruption

Remove 'Save to SharePoint Site' Prompt from an Access Database

Class Not Registered Run-time Error -2147221164

Inconsistent Compile Error

Decompile Database

Bad DLL Calling Convention

Error 3045: Could Not Use

Converting ACCDB to MDB

SQL Server Upsizing

Microsoft Access to SQL Server Upsizing Center

Microsoft Access to SQL Server Upsizing Center

When and How to Upsize Access to SQL Server

SQL Server Express Versions and Downloads

Cloud and Azure

Cloud Implications

MS Access and SQL Azure

Deploying MS Access Linked to SQL Azure

SQL Server Azure Usage and DTU Limits

Visual Studio LightSwitch

LightSwitch Introduction

Comparison Matrix

Additional Resources

Connect with Us


Free Product Catalog from FMS