HELP: Run-time error 3219

Hello,

I am trying to insert a single, 1 column record into a table using ADO. Note that all variables have been Dim'd. Here is the code, with line #'s:

1. SqlInsertStr = "Insert into tblDummyS3ID (DummyS3ID) Values (NewMaxS3ID);"

2. recset.Open "tblDummyS3ID", CurrentProject.Connection, adOpenDynamic, adLockOptimistic

3. With recset
4. .Fields("DummyS3ID") = NewMaxNumber
5. .AddNew
6. .Update
7. End With
8. recset.Close
9. Set recset = Nothing


Currently, when line 6 executes, I get the following error:

"Index or primary key cannot contain a Null value."

The table has only 1 column, which is the PK. The variable newMaxNumber was set earlier in the code and the correct value to insert is confirmed. After the run-time error occurs, I stop execution of the code, go back to design view on the form, then look at the table. Each insert should add a new row, however, there is only 1 row, and that value is being updated. This is not necessarily bad because the end result is the same. However, this is not what I expect. I expect a new row to be added, not an update to the row which already exists.

Can anyone explain this behavior to me and how to resolve the 3219?

TIA,
Rich
[1386 byte] By [rstringer] at [2007-11-20 9:55:59]
# 1 Re: HELP: Run-time error 3219
if the keys are numeric, line 2 is ok.

reverse 4 & 5 and give it a try.
dglienna at 2007-11-9 19:35:56 >
# 2 Re: HELP: Run-time error 3219
Yes, reverse 4 & 5. Reason: when you '.AddNew' it will initialize fields to empty or null, resetting the assignment in 4 to null and therefore, gets an error on 6.
aio at 2007-11-9 19:36:56 >
# 3 Re: HELP: Run-time error 3219
Plz Change Your Code Into:
No need for Line 1.

2. recset.Open "tblDummyS3ID", CurrentProject.Connection, adOpenDynamic, adLockOptimistic

3. With recset
4. .AddNew
5. .Fields("DummyS3ID") = NewMaxNumber
6. .Update
7. End With
8. recset.Close
9. Set recset = Nothing
ShafaqatAli at 2007-11-9 19:38:00 >
# 4 Re: HELP: Run-time error 3219
Hello dglienna and aio,

Thanks to you both. This worked. I appreciate you explaining WHY this error occurs.

Cheers to you both!!!
rstringer at 2007-11-9 19:39:06 >