Results 1 to 6 of 6

Thread: obtaining record ID

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 1999
    Location
    New Jersey
    Posts
    334

    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?

  2. #2
    Hyperactive Member Anglo Saxon's Avatar
    Join Date
    Mar 2002
    Location
    Durham, UK
    Posts
    259
    What type of cursor are you using? You need to make sure it's adOpenKeyset.



    --
    AS

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 1999
    Location
    New Jersey
    Posts
    334
    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?

  4. #4
    Hyperactive Member Anglo Saxon's Avatar
    Join Date
    Mar 2002
    Location
    Durham, UK
    Posts
    259
    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

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 1999
    Location
    New Jersey
    Posts
    334
    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.

  6. #6
    Hyperactive Member Anglo Saxon's Avatar
    Join Date
    Mar 2002
    Location
    Durham, UK
    Posts
    259
    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

Posting Permissions

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



Click Here to Expand Forum to Full Width