VBA Error Handling PT2

In this VBA Error Handling set I’ll talk about using the error object

Here is a chart referencing the Error object’s properties for you to reference:

Property Description
Number

The number of the error.

Description The description of the error.
Source The name of the object or application that originally generated the error.

…CODE EXAMPLE

Public Sub ErrObjectSample()
    On Error GoTo errHandler
    
    'Causes error:
    x = 1 / 0
    
    Exit Sub
errHandler:
    
    'Select the case of the Err Object:
    Select Case Err.Number
        
        'Division by zero error:
        Case 11
            MsgBox "You cannot divide by zero.", vbExclamation, APP_NAME
            
        Case Else
            'Some other error occured, so show a generic error message.
            
            MsgBox "An error resulted", vbCritical, APP_NAME
            
    
    End Select
    
End Sub

“APP_NAME” is a constant I declared in the General Declarations section of the module. You may want to put all these declarations in a module by themselves.

Public Const APP_NAME =Erik's Project

<< VBA Error Handling PT1 | VBA Do Loop >>




By the way, if you got or are getting value from the VBA information, please click the "Donate" button to give me a small token of your appreciation, thanks!


These posts may help answer your question too...

Learn Access VBA: Understand Tables, Queries, Forms, and Reports

Learn Access VBA: From Zero to Database Hero If you’ve ever opened Microsoft Access and wondered how all the pieces fit together — tables, queries, forms, and reports — this tutorial is made for you. In just a few minutes, you’ll understand how Access works behind the scenes and see how VBA (Visual Basic for […]

How To Parse A Flat File In Excel VBA

In another post I demonstrated how to access a file on your computer using the MS Office Library. Here it is if you don’t know what I’m talking about. In this post, I am going to show you how to access the file and load it into your spreadsheet. I will do the same thing […]

How do I handle errors in Access VBA code?

I am going to give you the answer to “How do I handle errors in Access VBA code?” In my opinion there are 2 ways to handle errors: 1. Avoid the potential for an error to occur. 2. Handle the error in your code. Number 1 – If you have a control on your form […]

What Is Microsoft Access Used For?

To those of you who are asking the question of “What is microsoft access used for?” , here is my opinion. I’ve been using this program for well over 15 years, and it’s become fairly easy to deal with. Many people feel that it is hard to work with, but that’s not my experience anymore […]


Support these sponsors:
Previous Post

VBA Do Loop

Next Post

VBA Error Handling PT1

Leave a Reply

Your email address will not be published. Required fields are marked *