Handling Errors in Nested Procedures

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

When an error occurs in a nested procedure and I force VB to serach backward
through the calls i.e. From Procedure D to Procedure C to Procedure B to
Procedure A -
how can I tell Procedure A was my first call?
how can I then get it to display a particular message without having the
err.description message displayed.
 
Unfortunately, VBA does not expose the procedure stack. Heck, it doesn't
even expose the name of the procedure that generated the error.

The simplest solution is to use error handling in every procedure, and
handle them independently. Makes debugging much easier.

For those utility procedures where you want to pass an error message back to
a calling procedure, pass in a string to concatenate the error message to.

This example moves a recordset variable forward one record, returning True
on success, and returning an error message if the calling procedure cannot
continue to use the recordset. It has its own error handling, but relies on
the calling procedure to display any error message. (This is not a good
purpose; just an example.)

Private Function MoveRS(rs As DAO.Recordset, strError As String) As Boolean
On Error GoTo Err_Handler

rs.MoveNext

If rs.EOF Then
strError = strError & "End of recordset"
Else
MoveRS = True
End If

Exit_Handler:
Exit Function

Err_Handler:
strError = strError & Err.Description & vbCrLf
Resume Exit_Handler
End Function

Public Function Main()
Dim rs As DAO.Recordset
Dim strError As String
Set rs = DBEngine(0)(0).OpenRecordset("Table1")

Do While Not rs.EOF
If MoveRS(rs, strError) Then
'do your stuff
Else
MsgBox strError, vbExclamation, "Oops!"
Exit Do
End If
Loop

rs.Close
Set rs = Nothing
End Function
 
Back
Top