Solved

Get ID of record you just inserted

Posted on 2009-04-03
4
373 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 143

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 143

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

726 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