Databases Reference
In-Depth Information
line just before the ERR_EXAMPLE label. Without this statement, the error-handling code
will always be executed, even when there is no error! Omitting this line is a common
mistake. Note also that labels always end with a colon.
The process of adding error-handling code to a procedure is sometimes referred to as
error-trapping .
12.5.2 Handling Errors in the Calling Procedure
Consider the following version of the RecordCt function:
Function RecordCt(TableName As String) As Integer
On Error GoTo ERR_EXAMPLE
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset(TableName)
RecordCt = rs.RecordCount
rs.Close
Exit Function
ERR_EXAMPLE:
RecordCt = -1 ' Indicates error
rs.Close
Exit Function
End Function
In this case, if there is an error, the function will simply return the value -1 , rather than
displaying a message box. This behavior is better than that of the previous version,
because in this case the calling procedure can decide what to do.
Here is a procedure that calls RecordCt :
Sub Main( )
On Error GoTo Err_Main
Dim rc As Long
rc = RecordCt("Object")
If rc = -1 Then
' code here to handle error
Else
' code here for no error
End If
Exit Sub
Err_Main:
MsgBox "Error " & Err.Number & " - " & Err.Description, vbCritical
Search WWH ::




Custom Search