I need to insert a record into an Access database and get a handle on the primary key value for that record at the same time. The insertion part is easy, but how do I return the primary key value?
The primary key, which is an AutoNumber field, is the only unique value so I can’t do another SQL statement based on the inserted data, it will bring up multiple records.
It sounds like you have a bad table design if it has an arbitrary/sequence number for a primary key and no other unique item(s). It defeats the purpose of a relational database for a row to have no unique properties that make it different from all the other rows. C.B.'s suggestion will work but you have bigger problems.
Contact me away from the SDMB if I can help with this.
One (non-elegant) way to do it would to put some kind of suffix on the data (such as “-updated” or “999999”) that you insert so that you can identify it in the second query. Then simply run a third query to get rid of the suffix. What the suffix is depends on what type of field you are inserting into. Again, this is not very elegant, but if you need a one-shot deal, then this is a quick fix.
Winky, I got your email so I see what you’re trying to do. I still think that having a real property as the key is a better way to go. I’m an Oracle geek and we have some more flexibility in dealing with generated sequences for keys.
I think that adding the user’s name in combination with a time stamp, maybe rounded off to the minute, for a unique identifier in addition to your numeric key. SDMB doesn’t allow a single user to post twice in less than 60 seconds so this might work without changing your original design too much.