Solved

Get ID of record you just inserted

Posted on 2009-04-03
4
370 Views
Last Modified: 2012-05-06
How do I capture the ID of the record I just inserted into DB.

column name is ID.
SQL = "INSERT INTO homeFormUsers (firstName,lastName,emailAdd, ipAddress, interestedIn, datesubmitted)" 

SQL = SQL & "VALUES ('" & strFname & "','" & strLname & "','" & strEmailAdd & "','" & strIP & "','" & strIntersted & "','" & strCurrDateTime & "')"

objConn.execute SQL

Open in new window

0
Comment
Question by:pGustafson
  • 2
  • 2
4 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 24060966
this should do (presuming it's a identity
SQL = "SET NOCOUNT ON; INSERT INTO homeFormUsers (firstName,lastName,emailAdd, ipAddress, interestedIn, datesubmitted)" 
SQL = SQL & "VALUES ('" & strFname & "','" & strLname & "','" & strEmailAdd & "','" & strIP & "','" & strIntersted & "','" & strCurrDateTime & "'); SELECT SCOPE_IDENTITY() ID "
dim rst
set rst = objConn.execute(SQL) 
response.write rst.fields("ID").Value

Open in new window

0
 

Author Comment

by:pGustafson
ID: 24061038
When I use that I get:

ADODB.Recordset error '800a0cc1'
Item cannot be found in the collection corresponding to the requested name or ordinal.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24061088
you must have applied the suggested change incompletely.
either the SET NOCOUNT ON is missing, or the ID after SCOPE_IDENTITY() ...
0
 

Author Closing Comment

by:pGustafson
ID: 31566283
Thanks that worked!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now