Re: Is David Fenton right about error handling?
- From: Bri <not@xxxxxxxx>
- Date: Wed, 04 Jan 2006 17:44:13 GMT
Anthony England wrote:
OK I know I am cheating, by now choosing to use ADO instead of DAO, but consider version 4. It is similar to the version 2 which you approve of. This function works fine as long as tblContact is there, but if I rename the table to tblBogus for instance, I get stuck in a never-ending loop caused by me trying to shut the recordset if it is not nothing (should have checked if it was actually open).
Now I don't know whether there is any way you can have a DAO recordset which 'is not nothing' but either not to be open or to be un-closable due to some other reason (** can anyone think of an example??? **). However, this is possible with ADO and I am concerned with the general principal of guaranteeing that exit code always exits and never-ending loops cannot occur.
If you are concerned about errors occurring in your error/exit handlers you could use the On Error Resume Next statement, but then follow each subsequent statement with a test for Err.Number<>0 to verify that there was no error. This also a useful technique for dealing with errors that you want to test for at specific points in your code rather than just in general.
So, using your last example as a starting point and showing this technique in both places that I have mentioned (note that this and the other examples are to demonstrate the error handling techniques under discussion only):
Public Function ContactExists4(lngConID As Long) As Boolean
On Error GoTo Err_Handler
Dim rst As ADODB.Recordset
Dim strSQL As String strSQL = "SELECT tblContact.* FROM tblContact WHERE " & _
"ConID=" & CStr(lngConID) On Error Resume Next 'want to know that the error occured here
Set rst = New ADODB.Recordset
If Err.Number<>0 Then
MsgBox "Could NOT Set the Recordset"
'Do code that you need to fix this specific error
'and then continue or goto Exit as appropriate
End If
On Error GoTo Err_Handler 'go back to general error handling
rst.Open strSQL, CurrentProject.Connection If Not rst.EOF Then
ContactExists4 = True
End IfExit_Handler:
On Error Resume Next
If Not rst Is Nothing Then
rst.Close
If Err.Number<>0 Then
MsgBox "Could NOT close the Recordset"
End If Set rst = Nothing
If Err.Number<>0 Then
MsgBox "Could not Set rst = Nothing "
End If
End If
On Error Resume 0
Exit FunctionErr_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_HandlerEnd Function
-- Bri
.
- References:
- Is David Fenton right about error handling?
- From: Anthony England
- Re: Is David Fenton right about error handling?
- From: Bri
- Re: Is David Fenton right about error handling?
- From: Anthony England
- Is David Fenton right about error handling?
- Prev by Date: Re: Problems while making MDE
- Next by Date: Re: which approach? (cofused with tables)
- Previous by thread: Re: Is David Fenton right about error handling?
- Next by thread: Re: Is David Fenton right about error handling?
- Index(es):
Relevant Pages
|
Loading