Addition to: https://www.access-programmers.co.uk/forums/threads/menus-on-a-form.326893/post-1868023
I'm starting a new thread since this doesn't fit the actual topic of the linked thread.
However, since I always see a similar structure in error handling, I wanted to show an example of how not to implement error handling.
This error handling is incorrect:
Is the error of the error handling recognizable?
The solution is simple:
Either pass the error up or include error handling for unexpected/unresolved errors in the top-level procedure only.
I'm starting a new thread since this doesn't fit the actual topic of the linked thread.
However, since I always see a similar structure in error handling, I wanted to show an example of how not to implement error handling.
This error handling is incorrect:
Code:
Public Sub TestMe()
On Error GoTo ErrHand
CalcAndSaveResult 2, 1
ExitHere:
Exit Sub
ErrHand:
HandleError "TestMe"
Resume ExitHere
End Sub
Private Sub CalcAndSaveResult(ByVal a As Long, ByVal b As Long)
Dim result As Long
Dim InsertSql As String
On Error GoTo ErrHand
result = CalcResult(a, b) '<--- an error occurs in this procedure and an incorrect result is returned.
' From here on there is no error => code continues to run as if nothing had happened.
InsertSql = "insert into TableABC (a, b, R) Values (" & Str(a) & ", " & Str(b) & ", " & Str(result) & ")"
MsgBox "Result (a-b)/(b-1) = " & result & vbNewLine & vbNewLine & InsertSql
' ^
' |.. dummy replacement for data operation e.g:
' CurrentDb.Execute InsertSql, dbFailOnError
' Save 0 is correct?
ExitHere:
Exit Sub
ErrHand:
HandleError "CalcAndSaveResult"
Resume ExitHere
End Sub
Private Function CalcResult(ByVal a As Long, ByVal b As Long) As Long
On Error GoTo ErrHand
CalcResult = (a - b) \ (b - 1)
ExitHere:
Exit Function
ErrHand:
HandleError "CalcResult"
Resume ExitHere
End Function
Private Sub HandleError(ByVal ProcName As String)
' dummy replacement for error logging (save in text file, table, ...)
MsgBox "Error " & Err.Number & " in " & ProcName & ": " & Err.Description
End Sub
Is the error of the error handling recognizable?
The solution is simple:
Either pass the error up or include error handling for unexpected/unresolved errors in the top-level procedure only.
Last edited: