DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Abdel K. Belkasri Guest

    Return the newly created ID


    Hi there!

    This must be easy for those of you who are familiar with VB and DB.

    Suppose I have the following Table (In MS Access):

    tbCustomers
    CustomerID: Auto-number
    CustomerName: String

    I use a piece of code of this nature to insert a new customer:

    strSQL = "INSERT INTO tbCustomers Values (CustomerName,'IBM')"
    db.Execute (strSQL)

    How do we get back the value of the newly created CustomerID (Auto-number)?

    Do we read the database and look for the last inserted element? (another
    person may have inserted another row just after I inserted mine!).

    Thanks in advance.

    --Abdel.


  2. #2
    Patrick Escarcega Guest

    Re: Return the newly created ID

    Adbel,

    This is not the best method, and it can introduce race conditions
    with multiple users. It also assumes that the Auto-Number field
    increments.

    <just p-code>
    NewID= SELECT MAX(CustomerID) FROM tbCustomers WHERE
    CustomerName=<customername>

    You could also use recordsets and refresh the data after the update.

    --
    Patrick Escarcega patrick AT vbguru DOT net
    Consultant:Maxim Group www.vbguru.net
    ---------Do not write below this line---------------

    "Abdel K. Belkasri" <abdelb@riseintl.com> wrote in message
    news:38cd07ae$1@news.devx.com...
    > Suppose I have the following Table (In MS Access):
    >
    > tbCustomers
    > CustomerID: Auto-number
    > CustomerName: String
    >
    > I use a piece of code of this nature to insert a new customer:
    >
    > strSQL = "INSERT INTO tbCustomers Values (CustomerName,'IBM')"
    > db.Execute (strSQL)
    >
    > How do we get back the value of the newly created CustomerID

    (Auto-number)?




Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links