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 give me a tip, thanks!


These posts may help answer your question too...

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 […]

How To Make An Access Select Query

Hi everyone, This is not really a VBA item, but it’s important and you will need to understand this concept when you are creating your SQL statements in VBA. In our example we have a table called “Customers”: Now we just want the customers from “London”. So we set up a select query to select […]

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 *