Results 1 to 5 of 5
  1. #1
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Alzano Lombardo, Italy
    Posts
    1,483
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Adding New Records to a Recordset (2000)

    I'm trying to add days to the Giorno column of the Forecast table in the attached mdb using the AddDays procedure in the Module1 module

    <font color=red>Public Sub AddDays()
    Set cnn = CurrentProject.Connection
    rst.Open "Forecast", cnn, adOpenKeyset, adLockOptimistic
    rst.MoveLast
    rst.MoveFirst
    For i = 1 To 24
    rst.AddNew
    rst!Giorno = #7/31/2004# + i
    rst.Update
    Next i
    rst.Close
    Set rst = Nothing
    Set cnn = Nothing
    End Sub</font color=red>

    but I get the following error message:
    run-time error '-2147217887 (80040e21)': Index or primary key cannot contain a Null value.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Adding New Records to a Recordset (2000)

    Your table has a primary index on Giorno and Ora, so you must supply a value for both fields. Neither field in a primary index can be empty (null). You can add

    rst!Ora = 0

    to create a record with Ora = 0, or you can add a loop to create records for Ora = 0, 1, ..., 23 for each day:

    Public Sub AddDays()
    Dim i As Integer, j As Integer
    Set cnn = CurrentProject.Connection
    rst.Open "forecast", cnn, adOpenKeyset, adLockOptimistic
    ' rst.MoveLast
    ' rst.MoveFirst
    For i = 1 To 23
    For j = 0 To 23
    rst.AddNew
    rst!Giorno = #7/31/2004# + i
    rst!Ora = j
    rst.Update
    Next j
    Next i
    rst.Close
    Set rst = Nothing
    Set cnn = Nothing
    End Sub

    Note: I changed the upper bound for the i loop to 23, since your table already contains records for 08/24/2004, so i = 24 would lead to duplicate records.

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Adding New Records to a Recordset (2000)

    I notice you edited your post, but I don't know if that invalidates my reply.

  4. #4
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Alzano Lombardo, Italy
    Posts
    1,483
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Adding New Records to a Recordset (2000)

    Thank you Hans.
    Everything you said still totally stands. I just capitalized the word Null in the error message <img src=/S/blush.gif border=0 alt=blush width=15 height=15>.
    By the way, how can I solve this?

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Adding New Records to a Recordset (2000)

    I have posted a reply in the other thread, but I don't know a solution.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •