Select Last Number In Access

I have recently been working on a ticketing system and i needed to know how to find they next number for the primary key. Took me a while to find out how to do it in access so i thought i would share it.

Here is this SQL that i used.

SELECT LAST([Tickets.TicketID])+1 AS TicketID FROM Tickets

I know that with many of the better DBMS they can return the generated keys, But from what i found you can not do this with access.

If you have found any easier methods then please feel free to share them.

Comments
dfsaf's Gravatar Use an autonumber column. It gets set automatically instead of you getting it first.
# Posted By dfsaf | 07/03/09 13:57
Jonny Shaw's Gravatar I needed to insert to 2 tables at the same time. I needed to know the TicketID from the 1st table to use it in the second.
But as i said in the post MS access wont allow you to return generated keys.
# Posted By Jonny Shaw | 07/03/09 14:10
Andrew Scott's Gravatar My question is simple, why MS Access?

Access is maybe better these days than it was many years ago. But there are far better databases to use than MS Access. Hell Coldfusion 8 comes with Derby and you can use other free DB's like mySQL or MS SQL Express to name a few.

It still amazes me why people still use MS Access, I see no good reason for it.
# Posted By Andrew Scott | 07/03/09 21:19
Jonny Shaw's Gravatar I was using it with a java application.
The only reason i used access was because it was a requirement in the specification.
I would have probably used oracle if it were up to me.
# Posted By Jonny Shaw | 08/03/09 23:24
dave's Gravatar If you use the autonumber column you can do a select max(id) from firsttable after you've done the insert to get the number you need for the second table, just do the insert and select inside a cftransaction
# Posted By dave | 06/04/09 19:26
Jonny Shaw's Gravatar I wasnt using coldfusion, i was using java
# Posted By Jonny Shaw | 06/04/09 19:36
Todd Rafferty's Gravatar Make one more column, insert a createuuid() into that column.. do a selection to get the id of the column where create_uuid = #the_uuid_you_just_inserted#.

Otherwise, you'll run into issues using this code when multiple people start using it.
# Posted By Todd Rafferty | 05/08/09 23:18
Jonny Shaw's Gravatar It will work fine if it is done in a transaction
# Posted By Jonny Shaw | 05/08/09 23:22

Archives By Subject

Advertising (4) [RSS]
Blog Design (1) [RSS]
CFProject Scripts (2) [RSS]
Coldfusion Charts (3) [RSS]
Coldfusion Functions (5) [RSS]
Coldfusion Overview (1) [RSS]
Coldfusion Tutorials (16) [RSS]
For Sale (2) [RSS]
Image Manipulation (1) [RSS]
Java (1) [RSS]
JavaScript (4) [RSS]
PHP (1) [RSS]
Railo (4) [RSS]
SQL (1) [RSS]
Useful Tools (3) [RSS]

Recent Comments

Simple Coldfusion Upload Script
Alban said: Here is the error I get on the cffile line: String index out of range: -1 [More]

Coldfusion 8 Functions
Mark said: Can Cold Fusion do math functions similar to say Excel? I need to make several calculations, and if ... [More]

BlogCFC on Railo
iphone clone said: Thanks for sharing [More]

Coldfusion Protx VSP Form Integration Kit
confinedspace said: @Dom: Cheers for that - helped me out. Just one thing to note, I would add "all" as a thir... [More]

Coldfusion to PHP?
Jonny Shaw said: PHP is very similar to most other OOP languages, which i am quite familiar with, so it wasn't really... [More]

Recent Entries

No recent entries.
ColdFusion Blog | ColdFusion Hosting | ColdFusion Q & A