get last record from database without looping

M

Matt

how to get the last record from database without looping?

Whenever the user need to insert a new record to the database, it just
increment the id field by one from the last record.

I tried objRS.MoveLast, but it wont work.
 
A

Aaron Bertrand [MVP]

how to get the last record from database without looping?

There isn't really any concept of "last." If you have a column that
indicates order without ambiguity, you can use one of these:

SELECT MAX(column) FROM table

SELECT TOP 1 * FROM table ORDER BY column DESC

Also see http://www.aspfaq.com/2499
Whenever the user need to insert a new record to the database, it just
increment the id field by one from the last record.

You're killing scalability and concurrency if you're expecting to select the
max(id), add 1, and use that as part of your insert. IDENTITY (SQL Server)
and AUTOINCREMENT (Access) are much better suited for this than rolling your
own.

On the other hand, if you're using IDENTITY or AUTOINCREMENT and are just
trying to retrieve the last value inserted, see http://www.aspfaq.com/2174
I tried objRS.MoveLast, but it wont work.

So wait, you're adding a record, and using objRS? Please read
http://www.aspfaq.com/2191

In the future, please tell us what database and version you're using,
include code samples as well as a more explicit description of the problem.
"wont work" doesn't give us much to go on. Do you get an error message? If
so, what is it? Or, how is it behaving that you think is wrong? How do you
think it should behave?

(I've read your message five times and I still have no idea if you're adding
rows or retrieving rows.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top