How To Change default error messages

G

Guest

Hi
This may be a basic question (no pun intended)
How do you change the errors that access displays, for example when a
required field on a form is not completed and the user tries to save the
record? I know how to use the validation rule for validation text but this
doesn't apply to required fields in a record. Also, how do I specify the
message to appear to confirm a delete?

TIA
Rich
 
F

fredg

Hi
This may be a basic question (no pun intended)
How do you change the errors that access displays, for example when a
required field on a form is not completed and the user tries to save the
record? I know how to use the validation rule for validation text but this
doesn't apply to required fields in a record. Also, how do I specify the
message to appear to confirm a delete?

TIA
Rich

Here's how you can find the correct error and show your own message
for any of the form level errors.

First code the Form's Error event:

MsgBox "Error#: " & DataErr ' Display the error number
Response = acDataErrDisplay ' Display Default message

Then open the form and intentionally make that error.

The message box will display the error number and the default error
message.

Next, go back to the error event and change that code to:

If DataErr = XXXX Then
Response = acDataErrContinue ' Don't display the default message
MsgBox "Please enter data in all required fields."
Else
MsgBox "Error#: " & DataErr
Response = acDataErrDisplay ' Display Default message
End If

where XXXX is the error number.
 
G

Guest

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
 
F

fredg

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.
 
G

Guest

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
 
F

fredg

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
 
G

Guest

Fredg,
I have used your code here to setup several custom error messages but one
still haunts me.
After one field has been updated,"CustomerID" I can roll my mouse wheel and
the Custom error message appears correctly but, I have 2 command buttons 1
for navigating to previouse Record and the other for the next Record. when I
click either one of them it says you can't co to the specified record. How
can I change this to my custom error message.
 
F

fredg

Fredg,
I have used your code here to setup several custom error messages but one
still haunts me.
After one field has been updated,"CustomerID" I can roll my mouse wheel and
the Custom error message appears correctly but, I have 2 command buttons 1
for navigating to previouse Record and the other for the next Record. when I
click either one of them it says you can't co to the specified record. How
can I change this to my custom error message.

This is not a form level error, but an error within the command button
click event.

Code the click event:
On Error GoTo Err_Handler
DoCmd.GoToRecord , , acPrevious
Exit_Sub:
Exit Sub
Err_Handler:
If Err = 2105 Then
MsgBox "Place your user friendly message here."
Else
MsgBox "Error #: " & Err.Number & " " & Err.Description
End If
Resume Exit_Sub

Do the same for the button that takes you to the Next record.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top