Solved

Connection objects and last_insert_id() help !

Posted on 2004-04-17
3
402 Views
Last Modified: 2012-05-04
Hey everyone,

I'm ripping my hair out over this problem:

I have a Insert statement, and i want to retrieve the last_insert_ID() after it has executed so it goes like this:

SQLINS = "INSERT INTO family_albums (category,fam_ID,album_name,hidden,system_album,in_progress,user_ID) values(null,'"&request.cookies("access")("family_ID")&"','"&Upload.Form("Albumselect")&Upload.Form("Albumcreate")&"',1,0,1,"&request.cookies("access")("user_ID")&")"
objCmd.CommandText = sqlins
objCmd.Execute
RS1.open "SELECT LAST_INSERT_ID() AS `album_ID`" , "DSN=xxxx; uid=xxxx;pwd=xxxx"
session("varalbum_ID") = RS1("album_ID")
RS1.close

That returns a RS1("album_ID") of 0.

HOWEVER, when i do
SQLINS = "INSERT INTO family_photos (fam_ID,addedby_user,album_ID,file_name) values("&request.cookies("access")("family_ID")&","&request.cookies("access")("user_ID")&",LAST_INSERT_ID(),'"&file.ExtractfileName&"')"
objCmd.CommandText = sqlins
objCmd.Execute

the last_insert_ID() works (HOWEVER, I can't use it because it is in a loop and would increment itself (when i dont want it to)

I think i've answered my own question.. but i still dont know how to make it work.
I'm asuming the problem is that i'm opening a whole new connection object and it's not finding any last inserted id's.

So my question if the above is right, how would i do a "SELECT" on a objcmd and return a value in a RS("") fashion ?

Thanks for any insight!\

Chris
0
Comment
Question by:garfield1979
3 Comments
 

Accepted Solution

by:
jameswalt earned 200 total points
ID: 10850723
I've had this problem many times. Since an insert does not have any return values, we could always execute a query such as follows:
This is the main problem with using autoincrements as primary keys (that and the additional storage required)

CONNECT.EXECUTE INSERT STATEMENT
RS1.OPEN "SELECT Max(album_ID) AS lastID FROM family_albums"

Response.write RS1("lastID")


I hope this helps - it's pretty much what you supposed, but a more clear solution.
0
 

Author Comment

by:garfield1979
ID: 10850752
Thanks James.

That solution wil work, i'm at nerves end and i'm about to throw this system against the wall..

:-D

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 10851342
The main problem with this approach is performance and in a multi-user environment you could pick up the wrong value.
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

Suggested Solutions

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/…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

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

16 Experts available now in Live!

Get 1:1 Help Now