Thanks Fred
I've got it working for one error. I'm a newcomer to VBA. How do I set
different messages for different errors? I've got something wrong in the
structure of the syntax... take the following example:
Private Sub Form_Error(DataErr As Integer, Response As Integer)
MsgBox "Error#: " & DataErr ' Display the error number
Response = acDataErrDisplay ' Display Default message
If DataErr = 2237 Then
Response = acDataErrContinue ' Don't display the default message
MsgBox "There is no vehicle with this VRM in the database. Please check
and re-enter VRM or select a vehicle from the list."
Me.Combo112.Value = ""
ElseIf DataErr = 3314 Then
Response = acDataErrContinue
MsgBox "Please complete are required fields for the vehicle or cancel
adding the new vehicle"
Else
MsgBox "Error#: " & DataErr
Response = acDataErrDisplay ' Display Default message
End If
Does it look like I've got all the If / Else / ElseIf / End If right and is
the syntax ok here?
Thanks Fred
Re:> I've got something wrong in the structure of the syntax.<
The fact that something is 'wrong' doesn't tell us much.
It's always best to state WHAT happens (or doesn't happen).
This has to be done in two steps.
First you do this part. Code the error handler:
Private Sub Form_Error(DataErr As Integer, Response As Integer)
MsgBox "Error#: " & DataErr ' Display the error number
Response = acDataErrDisplay ' Display Default message
End Sub
Then you open the form and intentionally make that error.
Take note of the error number.
Go Back to the Form error event and delete the previous code.
Change it so it now reads like this:
Private Sub Form_Error(DataErr As Integer, Response As Integer)
If DataErr = 2237 Then
Response = acDataErrContinue ' Don't display the default message
MsgBox "There is no vehicle with this VRM in the database." _
& vbNewLine & " Please check and re-enter VRM or select a vehicle" _
& " from the list."
Me.Combo112.Value = Null
ElseIf DataErr = 3314 Then
Response = acDataErrContinue
MsgBox "Please complete are required fields for the vehicle" _
& vbNewLine & "or cancel adding the new vehicle."
Else
MsgBox "Error#: " & DataErr
Response = acDataErrDisplay ' Display Default message
End If
End Sub
You might wish to consider the fact that you might raise that 2237
error from some other control on the form. If that occurs, using your
code, regardless of the control that caused the error it will be
Combo112 that will have it's data erased, not the control that caused
the error.
Change
Me.Combo112.Value = Null
to:
Me.ActiveControl = Null
and the text in whichever control caused the error will be erased.