I'm using SQLOLEDB provider through ADODB to work with a SQLServer 2012R2 database using VBA running in Excel.
I have no problems navigating through a RecordSet generated from a SELECT statement generated through a RecordSet.Open method.
I recently started having a RecordSet returned from INSERT statements through the use of an OUTPUT clause to return the ID for a newly inserted Record. I then call the Connection.Execute method with the SQL statement and obtain a RecordSet from this with the new ID value.
If the table in which the new record was inserted has Primary Key(s) defined, then trying to call RecordSet.MoveFirst on this returned RecordSet results in the Primary Key Violation exception being thrown as a VBA Runtime error. If there are no Keys defined on the table, then I do not get the error.
I'm getting the error in a generic function that I wrote which iterates through the record set to pull out data into an application-specific object.
I'm using the RecordSet.MoveFirst method prior to looping through the RecordSet to make sure that I'm at the beginning of the RecordSet, just in case the RecordSet had been touched by another method that changed the cursor position.
I also call the MoveFirst method in another function that gets the number of Records in the RecordSet, since I couldn't find any other way with the default cursor to find out how many records there are. The exception is raised in either of these two methods, whenever MoveFirst is called.
I noticed that the RecordSet returned from the OUTPUT Clause has the .Source property set to the full INSERT statement that was originally issued.
The property RecordSet.LockType is adLockReadOnly and .EditMode is adEditNone.
One thing that I don't understand here is why MoveFirst would result in this behavior, where it seems to be trying to repeat the INSERT statement just because I'm moving the cursor. I don't think it's actually doing that, because when I try this on a table with no keys defined I do not get an extra duplicate record being inserted.
Advice? Perhaps there is a better way to manage cursor positioning and iterating through the RecordSet?
I suppose I could try placing an "On Error Resume Next" statement before calling MoveFirst, but that doesn't seem like a good solution, even if it would work.