Index or Primary Key Cannot Have A Null Value

  • Thread starter chris1 via AccessMonster.com
  • Start date
C

chris1 via AccessMonster.com

I have a form that displays all the fields including the primary key field.
Currently if a user enters information into each field and then decides to
exit the form without entering a primary key value then Access displays an
error message box:

"Index or primary key cannot contain a Null value". (Error 3058).
Then it says it can't save the record and requests whether to close the form
or not.

I understand the error but how can I disable those alert boxes so that if the
user exits the form without entering a value in the primary key field, the
errors will not appear. What can I do to disable those message error alerts.
Thank you
 
A

Allen Browne

You don't mind if the user just loses their entry, without even being
notified?

How about giving them a Cancel button, that undoes any changes to the record
being entered/edited, and closes the form? The Event Procedure to use in the
Click event of the button would look like this:

Private Sub cmdCancel_Click()
If Me.Dirty Then
Me.Undo
End If
Me.Close acForm, Me.Name
End Sub
 
N

Naeem Azizian

You need to check if the primary key field is null before saveing the
record do so by puting a code in "Before Update" event of the form:

Private Sub Form_BeforeUpdate(Cancel As Integer)

If isnull( me.WhateverID) = true Then
msgbox "You did not provide a value for primary key!" & vbcr & vbcr
& "Record is not saved"
Cancel = True
Exit Sub
End If

End Sub

this continues until user provides a value for primary key or cancels
adding the new record.
 
C

chris1 via AccessMonster.com

What happens if the user closes the form using the "x" button in the upper
right corner (windows close method)? I still get the error messages . I know
I can place buttons to cancel but wwnat to make sure that no messages appear
when the "X' button is clicked on the upper right corner of the form? What
can I do?

Thank you!
 
C

chris1 via AccessMonster.com

I have found the Form's Close Button property and set it to No. Allen I used
the cancel button to undo the user's entry and exited out of the form. Thank
you!
 

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