Results 1 to 8 of 8
  1. #1
    kowalski is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Oct 2012
    Posts
    10

    Error on AddNew to an empty recordset

    Hi,
    I have the following code that is returning a "Runtime error 3058, Index or primary key cannot contain a Null value". Error happens on the Update.

    Set db = CurrentDb
    Dim rstWattage As Recordset
    Set rstWattage = db.OpenRecordset("SELECT [Circuit Operating Load] FROM tblLineID where tblLineID.[Line ID] = '" & strLineID & "'")


    If rstWattage.RecordCount = 0 Then
    With rstWattage
    .AddNew
    ![Circuit Operating Load] = "0"
    .Update
    End With
    End If

    Any ideas?

    Thanks in advance!

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    It sounds like there's a required field that isn't being populated.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    kowalski is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Oct 2012
    Posts
    10
    Right ... but the recordset is being created from
    SELECT [Circuit Operating Load] FROM tblLineID where tblLineID.[Line ID] = '" & strLineID & "
    so should it not just have that 1 field?


  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    It needs to include whatever fields are required in the table, other than an autonumber field.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    kowalski is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Oct 2012
    Posts
    10
    Quote Originally Posted by pbaldy View Post
    It needs to include whatever fields are required in the table, other than an autonumber field.
    OK, to be clearer, I don't want to affect any change on the underlying table. i.e. I want to create a recordset based on some values returned from a query and then manipulate that recordset (AddNew) without updating the table that the initial query was run on.

    So the way I see it, the recordset should only have 1 field (Circuit Operating Load). I think you're right in that Access is complaining because my AddNew is actually trying to create a new row in tblLineID but I don't want it to touch tblLineID at all. I just want to add a new row to the recordset.

    Cheers.

  6. #6
    rzw0wr is offline I will always be a newbie
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Indiana
    Posts
    479
    Code:
    Set rstWattage = db.OpenRecordset("SELECT [Circuit Operating Load] FROM  tblLineID where tblLineID.[Line ID] = '" & strLineID & "'")
    Just open the table without a where clause.

    Set tstWattage = db.OpenRecordset("tblLineID")

    You are AddNew and it will add it to the end of the recordset.



    Dale

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I've only used AddNew to add records to the table, and to my knowledge that's its purpose. Dale seems to think it will work so I'll get out of the way.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    rzw0wr is offline I will always be a newbie
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Indiana
    Posts
    479
    Did it again. I missed this post.

    OK, to be clearer, I don't want to affect any change on the underlying table. i.e. I want to create a recordset based on some values returned from a query and then manipulate that recordset (AddNew) without updating the table that the initial query was run on.

    So the way I see it, the recordset should only have 1 field (Circuit Operating Load). I think you're right in that Access is complaining because my AddNew is actually trying to create a new row in tblLineID but I don't want it to touch tblLineID at all. I just want to add a new row to the recordset.
    Now I hope I understand. You want to look at the field, use the value for something BUT NOT store it in the table.

    Open the recordset with the where clause to find the record you want.
    Leave out both edit and AddNew AND do not use the Update.

    Close the recordset as usual.

    Paul, Don't leave me. It scares me when you do.

    Dale

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 03-06-2013, 03:52 PM
  2. Replies: 1
    Last Post: 11-13-2012, 07:25 PM
  3. Using Recordset rst.AddNew
    By dandoescode in forum Programming
    Replies: 1
    Last Post: 06-06-2012, 10:10 AM
  4. Replies: 2
    Last Post: 04-23-2012, 10:13 PM
  5. Access 2003 returns empty recordset
    By Leelers in forum Queries
    Replies: 0
    Last Post: 03-20-2009, 11:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums