We help IT Professionals succeed at work.
Get Started

How do I eliminate "Index or Primary Key cannot contain a null value?"

sarahellis
sarahellis asked
on
3,571 Views
Last Modified: 2012-05-11
I have a form that displays an Item.  It has a subform that should show all units with this item, and allow users to add units to the item.  I want the subform to show any records, who's item matches the record that the form is currently displaying.  TblRollout has a unique field "key" which is a combination of the UnitID and ItemID.  

The recordsource for the subform is: "select Unitid, item from tblRollout order by item" It is a dynaset.
I can't find a place to put the code that adds the record without getting the above error.  I've tried in the form Before Update, in the textbox Before Update, and lots of other places.  The record does actually get added to the table, right before the error appears.  I think it has to do with the subform trying to add the record on its own.  I'd really appreciate any help you can offer.  Thank you!

The code that adds the record is:
    Dim rs As Recordset
    Set rs = CurrentDb.OpenRecordset("tblrollout", dbOpenDynaset)
    With rs
        If rs.RecordCount > 0 Then
          .MoveLast
        End If
        .AddNew
        !Item = Forms!frmUpdateTests1.txtTestid
        !UnitID = Me.[UnitID]
        !Key = Trim(Me.UnitID) & "-" & Trim(Forms!frmUpdateTests1.txtTestid)
        !Qty = 1
        !orddate = Date
        .Update
    End With
Comment
Watch Question
Top Expert 2007
Commented:
This problem has been solved!
Unlock 2 Answers and 19 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE