Error Handling

  • Thread starter Thread starter trungluu
  • Start date Start date
T

trungluu

Guest
Hi,

I have a field on the form. this is a required field. If I gave it a null value,I always got this message:
"The field 'PhieuNhapChiTiet.DVT' cannot contain a Null value because the Required propety for this field is set to true. Enter a value in this field."

I have used the error handling code to trap this error to change it to my own message. But it doesn't work well. the following is my code

Private Sub DVT_BeforeUpdate(Cancel As Integer)
On Error GoTo Err_handler:
MsgBox "Do before update"
Exit_handler:
Exit Sub
Err_handler:
MsgBox "show error"
Resume Exit_handler
End Sub

Could you please tell me are there any wrongs in my code? Should I use DVT_BeforeUpdate event procedure? How to trap this error?

Thanks in advance,

Trung Luu
 
If you are looking to put your own message when the null value occurs, why not put your own validation message in at the table level where your data is set to Required-Yes.

BL

[This message has been edited by boblarson (edited 01-14-2002).]
 
Hi Boblarson,

Thank you for your suggestion.

But I don't know why I cannot trap this error(there are some the similar error messages I cannot trap)? If I don't want to use validation rule and validation text properties how can I trap it? Because the validation text is only appeared when we enter a value prohibited by validation rule. I only set the required property to Yes.
Are these the system error messages? Are there any ways to trap and change this kind of error messages?

Thanks,
Trung Luu
 
Private Sub Form_Error(DataErr As Integer, Response As Integer)
If DataErr = 3201 Then
'Display Error Message
MsgBox "Your Message"
'Prevent the standard error message from showing
Response = acDataErrContinue

txtSomeField.SetFocus
Else
'If another error is applied then show the standard error message
Response = acDataErrDisplay
End If


End Sub
 
Hi Rich,

Thank you very much for your help.

Almost errors I can trap by your guide. But I can not trap this error when I close the form:

"You can't save this record at this time
Microsoft Access may have encountered an error while trying to save a record.
If you close this now the data changes you made will be lost.
Do you want to close the database object anyway?"

If the error is occured, I want to cancel the close event (keep the form) and don't show this message. Could you please tell me are there any ways to do this?

Thanks a lot,
Trung Luu
 
Hello,

Every error has a number. When the error occurs you see the error-number. Use the code of rich to trap all errors. Just fill in 'your' error code. So something like this:

Private Sub Form_Error(DataErr As Integer, Response As Integer)

select case err.number
case yournumber
msgbox "You haven't fill in all the data!"
case yournumber1
msgbox err.description
case else
msgbox err.number
msgbox err.description
end select

End Sub

You can put this code where ever you want. So also in the 'on close' event of a form.

Hope this will help.

Albert
 

Users who are viewing this thread

Back
Top Bottom