Home   Free Applications   Code Snippets   Fun Stuff 
 
 You are here: Home > Code Snippets > ASP
Register   Login  

 
Site Navigation


Print Print this page
Email E-mail this page
Bookmark Add to Favorites

 
Return Primary Key on Insert

Posted by on Friday, October 17, 2003 (EST)

This is the correct syntax to return the primary key from the table after an insert.

This is the correct syntax to return the primary key from the table after an insert.

Dim cn
Dim rs
set cn = CreateObject("ADODB.Connection")
cn.open "ODBCSorce", "LoginName" ,"LoginPass"
set rs = CreateObject("ADODB.Recordset")
rs.open "TableName", cn, adOpenKeyset, adLockPessimistic, adCmdTable
rs.AddNew
rs("FieldName") = "Test"
rs.Update
 
ID = rs("ID")
 
rs.close
set rs = nothing
cn.close
set cn = nothing

ASP NOTE: "adovbs.inc" must be included to work

<!--#include file="includes/adovbs.inc"-->


Comments:

doesn't work!
By ? on Friday, December 12, 2003 (EST)
This doesn't work on SQL 2000 using OLEDB.  There are problems with getting identity field through ADO

Reply to this Comment

MoveLast
By ? on Wednesday, January 28, 2004 (EST)

In the past I've had to use:

Dim cn
Dim rs
set cn = CreateObject("ADODB.Connection")
cn.open "ODBCSorce", "LoginName" ,"LoginPass"
set rs = CreateObject("ADODB.Recordset")
rs.open "SELECT * FROM TABLENAME WHERE ID = 0", cn, adOpenKeyset, adLockPessimistic
rs.AddNew
rs("FieldName") = "Test"
rs.Update
rs.MoveLast
 
ID = rs("ID")
 
rs.close
set rs = nothing
cn.close
set cn = nothing

Never fails for me, I've used this for Access, MS SQL 2000 and Oracle 9i

Paul

Reply to this Comment

Add Your Comment



Top
 
CNET News.com


Tech news and business reports by CNET News.com. Focused on information technology, core topics include computers, hardware, software, networking, and Internet media.

  • Security Bites 113: The security of Chrome
    Billy Hoffman of HP Web Security talks about Google's new browser, how it handles Javascript and what it means for Web 2.0 security.

  • Are Demo and TechCrunch50 fragmenting their audiences?
    With both events scheduled to start Monday, many press, as well as venture capitalists and others are having to choose which one to attend.

  • A user guide to following DemoFall and TechCrunch50
    More than 100 products are going to be introduced during the week of September 8. Will they be any good?

  • What's McCain doing in front of my junior high?
    Speculation has it that it was a goof-up that had the Republican nominee standing in front of Walter Reed Middle School during his acceptance speech. In any case, the image brings back memories for CNET News' Ina Fried.

  • Microsoft tries to reclaim Windows' image
    After years of letting Apple's attack ads go unanswered, software maker sets out on difficult, costly journey of trying to take back control of what Windows stands for.


    Copyright 1995-2008 CNET Networks, Inc. All rights reserved. Last Refreshed 9/5/2008 3:27:04 PM
  •