Index or Primary Key Cannot Have A Null Value

Index or Primary Key Cannot Have A Null Value

Post by chris1 via » Tue, 28 Nov 2006 11:48:43


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

--
Message posted via http://www.yqcomputer.com/
 
 
 

Index or Primary Key Cannot Have A Null Value

Post by Allen Brow » Tue, 28 Nov 2006 12:29:20

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

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://www.yqcomputer.com/
Reply to group, rather than allenbrowne at mvps dot org.

 
 
 

Index or Primary Key Cannot Have A Null Value

Post by Naeem Aziz » Tue, 28 Nov 2006 13:21:00

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.
 
 
 

Index or Primary Key Cannot Have A Null Value

Post by chris1 via » Tue, 28 Nov 2006 22:54:39

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!

--
Message posted via http://www.yqcomputer.com/
 
 
 

Index or Primary Key Cannot Have A Null Value

Post by chris1 via » Wed, 29 Nov 2006 10:29:27

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!

--
Message posted via AccessMonster.com
http://www.yqcomputer.com/