Error Handler

D

Duane

I have a sub form that has a field (Reason - combo box) linked to another
table. If a user doesn't enter a value in the combo box field, they receive
an error stating "You cannot add or change a record because a related
recordis required in table tblReason".

I would like to change the error messege but I am having trouble trying to
figure out where to trap the error. I have triend the Before Update and On
Error events of both the form and sub form, but I am still receiving the
standard Access run-time error.

Here is the code I am trying to use.

On Error GoTo Error_Handler

Exit_Procedure:
Exit Sub

Error_Handler:
If Err = 3201 Then
MsgBox "You must select a Reason from the drop down menu!"
Else
MsgBox "Error " & Err.Number & ": " & Err.Description
End If
Resume Exit_Procedure

Any help would be appreciated. Thanks
 
W

Wolfgang Kais

Hello Duane.

Duane said:
I have a sub form that has a field (Reason - combo box) linked to
another table. If a user doesn't enter a value in the combo box field,
they receive an error stating "You cannot add or change a
record because a related recordis required in table tblReason".

This message means that you only may store a value that comes from the
table tblReason. Since surely you have based the combo box on that
table, I guess that you have forgotten to delete the default value of
0 in the table that your form i s based on. Therefore, if a user does
not select a value in the combo box, the default value of 0 is to be
stored in the table. A referential integrity constraint does not allow
this, because - I bet that - the primary kay of tblReason is an
autoincrement field containing only values greater than 0.
I would like to change the error messege but I am having trouble
trying to figure out where to trap the error. I have triend the
Before Update and On Error events of both the form and sub form,
but I am still receiving the standard Access run-time error.

Suggestion: Remove the default value and use the before update event.
In that code, check if IsNull(Reason).
Here is the code I am trying to use.

On Error GoTo Error_Handler

Exit_Procedure:
Exit Sub

Error_Handler:
If Err = 3201 Then
MsgBox "You must select a Reason from the drop down menu!"
Else
MsgBox "Error " & Err.Number & ": " & Err.Description
End If
Resume Exit_Procedure

Any help would be appreciated. Thanks

I guess that this code comes from your BeforeUpdate procedure? As the
name suggests, it is executed before the update and therefore before
the error occurs, so there will be no error to trap in this procedure.
The error is not an error that occurs inside a procedure unless you
have explicitly tried to save the 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

Top