Microsoft Access 2010 Bible

(Rick Simeone) #1

Part III: More-Advanced Access Techniques


778


ErrorRoutine = 3
Case vbNo
ErrorRoutine = 4
End Select

A function to handle lock errors
Many of the routines in the earlier code listings call an error-handling function named
ErrorRoutine(). Instead of each procedure having to trap and interpret every possible error,
many developers condense error handling as a single public function (such as ErrorRoutine),
and have the function trap and handle errors as they occur. Each procedure in the application is
responsible for trapping errors, but the errors are passed to ErrorRoutine for handling.
ErrorRoutine may notify the user, log the error, or simply ignore the error, depending on the
details of the error incident.

The ErrorRoutine function (shown in Listing 21.7) uses the Err object’s Number property to
determine which locking error has occurred. It takes action based on the error number and the
TryCount parameter to appropriately handle record-locking problems. Most likely, you’ll want to
modify each error number to suit your users and their environment.

As you’ll see in Chapter 23, there is but a single Err object in any VBA project, such as a Microsoft
Access application. The Err object contains all the details of an error incident, and, because there
is only one Err object in the entire project, any routine (such as ErrorRoutine) can use the
information provided by the Err object.

ErrorRoutine accepts a single argument — TryCount — that instructs ErrorRoutine to
take specific actions depending on the value of TryCount. You saw this parameter passed to
ErrorRoutine in the previous sections.

LISTING 21.7
A Function to Handle Locking Errors

Public Function ErrorRoutine(TryCount As Long) As Integer
Dim lngCounter As Long
Dim lngReturn As Long
Dim strMessage As String
Select Case Err.Number
Case 3021
‘3021-No Current Record.
‘ Let the error pass
Case 3186
‘3186—Couldn’t Save; currently
‘locked by user x on machine y:
If TryCount < 10 Then
For lngCounter = 0 To 15000
‘Empty loop for short delay...
Free download pdf