Find Error Line Number in VBA With Erl

In this post you are going to find out a way of handling errors in your code with the Erl function.

Erl displays the last label before the error

Another alternative error handling solution is using labels at each point in long procedure.

Here we get a random error:

Here is the code behind the form:

Option Compare Database

Private Sub Command0_Click()
    GenerateError
End Sub


Private Sub GenerateError()

    On Error GoTo err_handler
10:
        Dim strForm As String
        
20:
        s = 2
        y = 4
30:
        strForm = "frmNothing"
40:
        DoCmd.OpenForm strForm, acFormDS
        

Exit Sub

err_handler:
    
    MsgBox "You have an error " & Err.Number & ": " & Err.Description & " (Error at label - " & Erl & ")"
       
    Exit Sub
End Sub

Since the form “frmNothing” doesn’t exist, we have an error at line 40.

Again: Erl displays the last label before the error which may be beneficial to you.

Let me know if you have any questions.


 




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

Here Is A Customized Msgbox VBA Example

Here is an example of a customized VBA Msgbox. We giving some richtext and customizable flair to the rather ordinary message box. The following code provided with the code after the screenshot, is going to provide you with the ability to really make the ordinary message box shine! Sub MsgboxVBAExamples() Dialog.Box “VBAHowTo.com is your source […]

How To Use The VBA Input Box Cancel Button To Exit Sub

This post will demonstrate how you can use the VBA input box cancel button to exit the sub procedure. When you click the “Cancel” button on the input box, you return a null (blank) value, and knowing this information, you can exit the sub procedure. Here is an example:   Sub VBAInputBoxCancel() Dim strResponse As […]

Previous Post

Mouse Click Counter On Access Form

Next Post

How To Open A Form To A Specific Record In Access 2016 VBA