Solved

Get newly created ID in SQL Server

Posted on 2008-10-22
12
416 Views
Last Modified: 2012-06-27
Hi

I am building an MS Access/SQL Server app.  If I create a new record  in the SQL Server backend using VBA behind an Access form how can I get the newly created ID and return it to Access?

Cheers
0
Comment
Question by:kenabbott
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 11

Expert Comment

by:Muhammad Kashif
ID: 22774411
Use
Select @@IDENTITY


You can use like this with your insert query

Insert into Table values(1,2,.....)
Select @@IDENTITY
0
 
LVL 22

Accepted Solution

by:
dportas earned 500 total points
ID: 22774570
Use SCOPE_IDENTITY() wherever possible rather than @@IDENTITY.

SCOPE_IDENTITY() returns the last inserted IDENTITY value in the current scope, which means its behaviour won't be affected by any current or future triggers on the table. If you are using SQL stored procs for data access then SCOPE_IDENTITY() should normally be preferred over @@IDENTITY.
0
 

Author Comment

by:kenabbott
ID: 22774728
So is the only way of doing this via a stored procedure??  Its not by getting a returned value with a pass thru query for example
0
 
LVL 84
ID: 22774911
How are you creating the new record? Show the relevant portion of the code.

Does the Jet/OLEDB provider support SCOPE_IDENTITY? I'm not sure ... I know it supports IDENTITY:

http://support.microsoft.com/default.aspx?scid=kb;EN-US;232144

But I've not found an article stating it supports SCOPE_IDENTITY. That said, you could of course get this via a Stored Proc or something ...
0
 

Author Comment

by:kenabbott
ID: 22774950
At the moment I'm still working on this but basically what I need to achieve is as follows...

1.  User enters data onto an access form
2.  Data saved in SQL as new record
3.  New ID returned
4.  New ID saved in a seperate SQL table (along with other data)

0
 
LVL 84
ID: 22774982
Again: How are you creating the new record? Are you using bound forms? If you are then your Form's Recordset will contain the new value after saving ...
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 

Author Comment

by:kenabbott
ID: 22774998
Sorry - no it is an unbound form so I will be using a SQL statement of some sort
0
 
LVL 84
ID: 22775031
I'd certainly agree with dportas as to using SCOPE_IDENTITY, but as I said earlier I'm not sure it's supported. You could do this:

Dim rst As ADODB.Recordset
Dim lNewID As Long

<Insert your record>

Set rst =New ADODB.Recordset
rst.Open "SELECT @@IDENTITY AS NewID", YourConnectionObject
lNewID = rst("NewID")

This could run into issues if you have a lot of users banging away on this, since as others have said @@IDENTITY gets the last inserted value - so if you insert your record, and then I insert a record immediately after that BEFORE YOUR CALL TO @@IDENTITY then you'll get the ID of the record I inserted. If you use a Stored Proc, you can use SCOPE_IDENTITY, which insures that you get the correct value.

0
 
LVL 22

Expert Comment

by:dportas
ID: 22777043
@@IDENTITY is scoped to the connection so you will never get the identity value of a row inserted by someone else unless you are connection pooling.

0
 
LVL 84
ID: 22777128
Good to know ...
0
 
LVL 84
ID: 23040940
So why did you accept the SCOPEIDENTITY answer? Is that what you ended up using?
0
 

Author Comment

by:kenabbott
ID: 23042464
yes
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…

911 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

17 Experts available now in Live!

Get 1:1 Help Now