handling error

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi I have a form with some text box in the Before_Update event of the key
filed I place this code

Private Sub EmpID_BeforeUpdate(Cancel As Integer)

If IsNull(Me![EmpID]) Or (Me![EmpID]) = "" Then
MsgBox "You must enter a Employee ID."
EmpID.SetFocus
Cancel = True
End If

End Sub

But let say I left empty the key field (EmpID) and I try to add the record,
in this case I got a system message Saying "Index or Primary key cannot
contain a null value" and the error # is 3058, how can I pick this error
number and sent a more a more friendly message instead of this system message.

thanks
 
To trap the error and replace the message, use the Error event of the form.

Alternatively, test if the control is null in Form_BeforeUpdate before
Access attempts to write the record.
 
As Allen said

Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(Me![EmpID]) Or (Me![EmpID]) = "" Then
MsgBox "You must enter a Employee ID."
EmpID.SetFocus
Cancel = True
End If
End Sub

Private Sub Form_Error(DataErr As Integer, Response As Integer)
If DataErr = 3058 Then 'The Primary Key is blank
Response = MsgBox("You Cannot Leave the Employee ID Blank!",
vbExclamation, "Employee ID id Blank")
Response = acDataErrContinue
End If
End Sub
 

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

Back
Top