Return the newly created ID
This must be easy for those of you who are familiar with VB and DB.
Suppose I have the following Table (In MS Access):
I use a piece of code of this nature to insert a new customer:
strSQL = "INSERT INTO tbCustomers Values (CustomerName,'IBM')"
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.
Re: Return the newly created ID
This is not the best method, and it can introduce race conditions
with multiple users. It also assumes that the Auto-Number field
NewID= SELECT MAX(CustomerID) FROM tbCustomers WHERE
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" <firstname.lastname@example.org> wrote in message
> Suppose I have the following Table (In MS Access):
> 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
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