 |
| 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
|
|
|
|

June 1st, 2005, 01:30 PM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

June 1st, 2005, 02:29 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
|
|
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
|
|

June 2nd, 2005, 07:29 AM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

June 2nd, 2005, 04:56 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
|
|
If you are using 2000 and above, it supports.
_________________________
- Vijay G
Strive for Perfection
|
|

June 23rd, 2005, 12:47 AM
|
|
Registered User
|
|
Join Date: Jun 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I think the problem is that MS Access can't do batch sql statements.
|
|

June 23rd, 2005, 11:00 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
|
|
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
|
|

June 23rd, 2005, 02:09 PM
|
|
Registered User
|
|
Join Date: Jun 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|
 |