Results 1 to 4 of 4

Thread: How to return ID?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    How to return ID?

    When inserting a record, I need to somehow return the AutoID that was generated for that record. I'm using ADO to connect to Access. What is the correct way of insuring that I return the correct ID? Thanks!

    Visual Studio 2010

  2. #2
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611
    If you are opening a recordset whit OpenKeySet, and you add a record, this autonumber field is already filled, with the correct number
    VB6 & C# (WCF LINQ) mostly


    If you need help with a WPF/WCF question post in the NEW WPF & WCF forum and we will try help the best we can

    My site

    My blog, couding troubles and solutions

    Free online tools

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091
    Sorry, I guess I wasn't clear. I'm executing an insert query, not opening a recordset. So how would I do it then? It is important to note that this is a multi user app meaning return MAX id is not going to work. I know I could do a timestamp check, but the problem with that is that it's entirely possible that 2 people insert a record within a few milliseconds of each other, thus causing a duplicate timestamp since a timestamp only goes down to seconds, correct? Also, there will be no user id envolved. So what's the best way for 1 particular client to retrieve the newly created ID?

    Visual Studio 2010

  4. #4
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    After the insert statement execute a Select @@Identity.

    VB Code:
    1. oConn.Execute("Insert Into ....")
    2.  
    3. Set oRsIdent = oConn.Execute("Select @@Identity")
    4.  
    5. debug.print oRsIdent(0).Value

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