Wrox Programmer Forums
|
Classic ASP Databases Discuss using ASP 3 to work with data in databases, including ASP Database Setup issues from the old P2P forum on this specific subtopic. See also the book forum Beginning ASP.NET Databases for questions specific to that book. NOT for ASP.NET 1.0, 1.1, or 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 1st, 2005, 01:30 PM
Authorized User
 
Join Date: Jun 2003
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Default Getting ID from inserted data in Access

I have 2 tables in which I need to store data from my ASP page. The first table (Students) contains the field ID that I need to insert into the 2nd table. I've tried using

"SELECT @@IDENTITY INSERT INTO Students (FirstName, LastName) VALUES ('Chris', 'Cote')"

but I keep getting and error that says:

Error Type:
Microsoft OLE DB Provider for ODBC Drivers (0x80040E14)
[Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression '@@IDENTITY 'ID' INSERT'.
/IMI/login.asp, line 38

Is there an equivalent call to using the @@IDENTITY statement in Access databases?


Chris
 
Old June 1st, 2005, 02:29 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Microsoft Access 2000 or later does support the @@IDENTITY property to retrieve the value of an Autonumber field after an INSERT.

You can't get identity value with the way you have used there.

"SELECT @@IDENTITY INSERT INTO Students (FirstName, LastName) VALUES ('Chris', 'Cote')"

It should be something like this.

"INSERT INTO Students (FirstName, LastName) VALUES ('Chris', 'Cote');
SELECT @@IDENTITY"

Hope that helps.

_________________________
- Vijay G
Strive for Perfection
 
Old June 2nd, 2005, 07:29 AM
Authorized User
 
Join Date: Jun 2003
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I just tried using "INSERT INTO Students (FirstName, LastName) VALUES ('Chris', 'Cote'); SELECT @@IDENTITY", but it didn't work. Now I get the following error:

Expected end of statement.

Is it possible that Access doesn't allow the IDENTITY property?

Chris

 
Old June 2nd, 2005, 04:56 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

If you are using 2000 and above, it supports.

_________________________
- Vijay G
Strive for Perfection
 
Old June 23rd, 2005, 12:47 AM
Registered User
 
Join Date: Jun 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I think the problem is that MS Access can't do batch sql statements.

 
Old June 23rd, 2005, 11:00 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

I don't know if this would work in Access but it works in SQL. Excuse me if I'm not understanding what you guys are talking about.

First insert into Students.
(This works in SQL.)

SQL = "Set nocount on;INSERT INTO Students (FirstName, LastName) VALUES ('Chris', 'Cote');Select TheID = @@identity;Set nocount off;"

Set oRS = oConn.Execute(SQL)
TheID = oRS("TheID")

Then use that 'ID' in your next insert statement. I don't know if Access uses the 'nocount' so you can try with and without the nocount statements.

Hope this helps.

Richard



 
Old June 23rd, 2005, 02:09 PM
Registered User
 
Join Date: Jun 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

After you insert the data into the first table, you can grab the top id using:

SELECT Max(StudentID) as MaxID FROM Students

... then you can insert MaxID on the next page... if I understand what you're trying to do





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Grab ID of a recently "inserted item" rsearing ASP.NET 2.0 Professional 11 February 15th, 2007 03:45 PM
how to avoid edit of inserted data in Excel? kotaiah Excel VBA 3 September 14th, 2006 01:49 AM
Last record inserted ID ADAC Programming VB Databases Basics 1 June 5th, 2006 02:41 PM
Get most recently inserted ID code mat41 Classic ASP Professional 17 May 2nd, 2006 02:19 AM
Data to be inserted into the database countmedream BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 0 November 2nd, 2004 03:19 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.