|
You saw earlier in this chapter that you can use error handling to trap record-locking errors when working with data in a recordset. But in many applications (including the TimeTrack sample database), users interact with forms rather than with recordsets. What then?
You have two choices for handling locking errors in forms. The first is to allow Access to handle things with its own default messages. If you elect this course of action, the messages shown in Figures 19.1 and 19.3 are presented to users when record-locking problems come up in a multiuser setting.
The alternative is to use the form's
Error
event. This event is raised whenever a data error happens in a form. For this case study, we've added some simple code to the Clients form to handle record-locking errors. Here's the code:
Private Sub Form_Error(DataErr As Integer, _
Response As Integer)
' Handle multi-user errors
On Error GoTo HandleErr
' Handle error based on error number
' passed in by Access
Select Case DataErr
Case 7787 ' Write conflict
MsgBox "Another user has changed this record. " & _
"Click OK to see their changes.", vbCritical
Response = acDataErrContinue
Case 7878 ' Data Changed
MsgBox "Another user has changed this record. " & _
"Click OK to see their changes.", vbCritical
Response = acDataErrContinue
Case Else
' Let Access handle the error
Response = acDataErrDisplay
End Select
ExitHere:
Exit Sub
HandleErr:
MsgBox "Error while handling errors", vbCritical
Resume ExitHere
End Sub
When you're working with the form's
Error
event, there are two arguments to deal with. The first,
DataErr
, is passed in by Access and includes the error number of the error that triggered the event. The second,
Response
, enables you to tell Access whether you've handled the error. You can pass back
acDataErrContinue
to tell Access that you've taken care of things, or
acDataErrDisplay
to tell Access to take its own default action (displaying the error).
In this particular case, you're trapping both varieties of locking error, warning the users that their data will get overwritten, and proceeding. Access automatically refreshes the form with the current data from the underlying database after this code runs. If any other data error (such as a validation error) occurs, this procedure just lets Access take its own default action. Finally, if an error occurs in the process of trying to handle some other error, the error handler displays a default message and bails out. This
prevents
the code from getting stuck in an infinite loop if something should go
badly
wrong.
|