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.


 


Comments are closed.