-
obtaining record ID
I am using ASP (with VBScript) to work with an Access database. I want to create a record, and then immediately obtain the auto-generated ID field for that record. I tried something like this:
rs.Open etc...
rs.AddNew
rs("name") = "spandex"
rs.update
recordid = rs("ID")
However, this doesn't work. It doesn't give me a value at all.
How can I do this?
-
What type of cursor are you using? You need to make sure it's adOpenKeyset.
--
AS
-
Well, I was not using that cursor. However, I still don't know how to go aout solving this. My code is as follows:
Code:
rs.Open Query,cn,adOpenKeySet,adLockOptimistic
rs.AddNew
rs("PostDate") = Date
eid = rs("ID")
rs.Update
I am trying to get the right value for eid, but I am always just getting a value of 0.
What am I doing wrong?
-
That's because you're trying to assign the value *before* you have used the update method.
Try this :
Code:
rs.Open Query,cn,adOpenKeySet,adLockOptimistic
rs.AddNew
rs("PostDate") = Date
rs.Update
eid = rs("ID")
--
AS
-
I've already tried that, doesn't work either.
Mayber some other part of my code is wrong. Could you perhaps test it with your own code, and see if it works for you?
Thanks a lot for the help.
-
I've used it hundreds of times and never had a problem. You'll have to double check everything like the Access DB field is definitely set to autonumber, variable spellings etc. etc.
Also, are you actually getting a 0 (zero) back or nothing at all?
If you still get no success, post all your code and Ill see if I can spot anything.
--
AS