Retrieve last inserted record

Question : Retrieve last inserted record

I am using Coldfusion MX, with an access database. Having just used a form to enter a record, I now want a query to pull off THAT record so I can link other data to it.. as primary key is an auto generated number I dont know what it is until the record is inserted.

I know I can query all the records in the table and order by PRIMARY KEY DESC, then just use a non repeated region query using the last record, but this seems a bit messy, with lots of unncessary query results.

Is there a way of pulling off JUST the last record in a table basically without knowing the key. I suspect MAXROWS = 1 will work, but just checking there is not a ‘proper’ way

Thanks


 

Solution: Retrieve last inserted record

The best way with access as your database is to have your cold fusion code as follows:

INSERT INTO mytable (field1, field2, etc) VALUES (#form.field1#, #form.field2#, etc)

SELECT MAX(RecordID) AS NewID FROM mytable

Then use newID variable from then on to pull the record you want.

As long as the web is the only way people will be inserting into the DB then the named lock will prevent others from adding until you have grabbed out the newly created RecordID.

(Just replace RecordID with your autonumberfield)