Cypher236
Cypher236

Reputation: 527

Getting a more useful/accurate error message

We have an issue with an Access front-end that seemingly randomly but constantly times out users with no explanation. This has been a long-standing issue, each time the disconnect happens we're provided with the sub that's causing the issue and a generic error message.

I'm starting to think the error message is misleading, it gives a generic "Connection Failure" with an Error Code of 0. After Googling the code that apparently means no error occurred?

I use C# not VBA so I'm not familiar with it but the error handling is as follows:

    Private Sub Form_Timer()

    Dim blnSystemMaintenance As Boolean

    On Error GoTo ErrHand

    ' Check for System Maintenance.
    blnSystemMaintenance = DLookup("SystemMaintenance", "ConfigItems")
    If blnSystemMaintenance Then

    //Do stuff
        Else
    //Do stuff
        End If

    //Do stuff

    End If

    ErrExit:
        Exit Sub
        Resume
    ErrHand:
    '    If Err.Number = 1 Then

    '    Else
            MsgBox "Error number: " & Err.Number & vbCrLf & _
           "Error description: " & Err.Description, vbCritical, _
           "Unexpected Error Occurred in Sub: Form_Timer"
    Resume ErrExit
    '    End If


    End Sub

Is there a way to get a more helpful error message?

Upvotes: 0

Views: 91

Answers (2)

Sergey S.
Sergey S.

Reputation: 6336

Sometimes error messages may be misleading for some type of functions like DLookup, this is a system problem, but sometimes error messages are misleading because not all procedures have correct error handlers and it's difficult to localize the problem especially if the developer has no direct access to the application. So, correct error handlers may significantly simplify troubleshooting.

Normally I use two types of error handlers, which give me quite clear diagnostic messages. Most common - usual error handler for procedures, which may interact with the user and normally called by the system - events handlers:

Private Sub Frame2_Click()
    On Error GoTo ErrorHandler
    'code here
ExitHere:
    On Error Resume Next
    Exit Sub
    Resume '>> remove in release
ErrorHandler:
    MsgBox "Error " & err.Number & " (" & err.Description & "), Line " & Erl & " in procedure Frame2_Click of Form_Form1", vbExclamation + vbMsgBoxHelpButton, "Error " & Now()
    Resume ExitHere
End Sub

The second type of handlers - for procedures, which may be called only by other VBA procedures, "bubble" handler, which adds information about the error to error description and passes the error up to caller procedure:

Sub MySub()
    On Error GoTo ErrorHandler
    'code here
ExitHere:
    Exit Sub
    Resume '>> remove in release
ErrorHandler:
    Debug.Assert Not (STOP_AT_ERROR And IS_DEV) '>> remove in release
    err.Raise err.Number, "MySub of Form_Form1", err.Description & vbCrLf & "in MySub of Form_Form1 at " & Erl
End Sub

It allows to show full call stack with error lines and procedure names. Line numbers should be added to each line of code though.

For debug simplifying I use two constants: if STOP_AT_ERROR is True and IS_DEV is True, code execution stops right after the error without bubbling and allows to inspect variables. For inspecting, I move the code execution pointer to the line Resume using Ctrl-F9, then press F8 and code pointer moves to the row, which caused the error.

In the production or QA versions just change IS_DEV to False and code won't stop at Debug.Assert line.

For automatic generating error handlers and adding line numbers, I use third party software, for instance, MZ-Tools. It can be done manually, but it's quite a time consuming

Upvotes: 1

Erik A
Erik A

Reputation: 32642

Domain aggregates often don't give descriptive error messages. I'm assuming your database runs into problems connecting to the backend when running the domain aggregate.

You can easily replace your domain aggregate with a recordset call:

blnSystemMaintenance = CurrentDb.OpenRecordset("SELECT SystemMaintenance FROM ConfigItems")(0).Value

Often, those give a bit more descriptive errors, if the error occurs at all when using a recordset.

Upvotes: 1

Related Questions