Sorry if I wasn't clear in my description in my last post... point taken.
I put in the code exactly as in your post and have got it all working ok. I
realised that I also have to code the "Save Record" button's onClick event
(command buttons don't have an OnError event) with the same error message as
3314 in the main form ("please complete all required fields....") because an
error when the user clicks the save button is not handled by the form's
OnError event... in other words, if the user didn't complete all fields, then
clicked the Save Record button, Access would still display the default
message rather than the message I'd stipulated in the form's OnError event.
This had been the "problem."
However, is it possible to selectively display custom error messages for the
save button? I can get the Save button to display the "Please Complete all
fields..." message by putting this in the Err_Save_Vehicle_Click part of the
VB as given below, and this works..... but (as presumably this will appear
for all errors relating to the button) if there should be any other error,
how do I revert to having default error message displayed?
The underlying reason seems to be that I can't seem to identify errors by
number; I cannot include "DataErr As Integer, Response As Integer" in the VBA
for a button as I can for a form.
ie including this in the opening line of the VB doesn't work:
Private Sub Save_Vehicle_Click(DataErr As Integer, Response As Integer)
.... access says the following error as soon as I put the form into form
view: "Procedure declaration does not match description of event or procedure
having the same name."
I tried including DataErr as Integer as when I include your code from your
previous post to identify error by number (MsgBox "Error#: " & DataErr) I
just get "Error#:" without any number.
Here is the complete code for the button:
Private Sub Save_Vehicle_Click()
On Error GoTo Err_Save_Vehicle_Click
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
Me.VRM.SetFocus
Me.Save_Vehicle.Visible = False
Me.CmdCancelNewVeh.Visible = False
Exit_Save_Vehicle_Click:
Exit Sub
Err_Save_Vehicle_Click:
Response = acDataContinue
MsgBox "Please enter all required fields for the vehicle" & vbNewLine &
"(VRM, make, model, colour and reason for stopping)" & vbNewLine & "before
saving or cancel adding the vehicle."
Resume Exit_Save_Vehicle_Click
End Sub
I hope this is clear. To recap, the save button dsiplays the "please
complete all fields" message if the fields aren't all completed, but the way
this is set up, it will display this message whatever error it occurs, as I
can't identify errors by number as I can in a form. Is there a way round
this to be "failsafe?"
TIA
Rich
There is no Response or DataErr arguments in the Click event.
Notice the difference between
Private Sub Form_Error(DataErr As Integer, Response As Integer)
and
Private Sub Save_Vehicle_Click()
therefore you cannot use DataErr or Response as you have.
The Form's Error event is written correctly.
Try the following in the Click event:
Private Sub Save_Vehicle_Click()
On Error GoTo Err_Save_Vehicle_Click
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, ,
acMenuVer70
Me.VRM.SetFocus
Me.Save_Vehicle.Visible = False
Me.CmdCancelNewVeh.Visible = False
Exit_Save_Vehicle_Click:
Exit Sub
Err_Save_Vehicle_Click:
If Err = XXXX '(Where XXXX is whatever the error number is)
MsgBox "Please enter all required fields for the vehicle" &
vbNewLine & "(VRM, make, model, colour and reason for stopping)" &
vbNewLine & "before saving or cancel adding the vehicle."
Else
' Display the error number and the generic Access message.
MsgBox "Error #: " & Err.Number & " " & Err.Description
End If
Resume Exit_Save_Vehicle_Click
End Sub