By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,419 Members | 1,609 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,419 IT Pros & Developers. It's quick & easy.

Go to previous record in sql query

P: 20
I am trying to get the previous record from a table.
Scenario is:
I have a previous button on the form. User browse through the records one by one to go back/forward.

For first time to pull data from sql,i check that if form is empty,if it is then i pull the last record form the table based on Max(id) which is autonumber.

Now, because people would be deleting record as well,there is autonumber not in any consecutive order.
Once i have got the lasr record, and when user click on previous button again, how can i pull the second last record and so on.

ANy suggestions would be a great help!!
Thanks!
Feb 4 '10 #1

✓ answered by ck9663

There are a lot of option for you. For one, you don't have to actually delete a record. You just need a flag to say the record is active or not. It should keep your record history.

If that's not an option, try this:
1. Display the current ID on the application.
2. User clicks NEXT
3. select top 1* from yourtable where CurrentID > IDOnYourTable order by ID desc.

This would be slow though, specially if your table is big.

Good Luck!!!

~~ CK

Share this Question
Share on Google+
5 Replies


P: 20
Once i have got the max(id),then to get the next last ID from the table, i need somethign like:

select * from table where id = (select max(id) - 'look for first mandatory filed not equal to zero' and get that id number)

how can i do that in sql?
Feb 4 '10 #2

P: 20
ODBC call failed error '3146'...
Feb 4 '10 #3

P: 20
How can i close this thread? i have opened another one for teh same problem in detail...
Feb 4 '10 #4

ck9663
Expert 2.5K+
P: 2,878
There are a lot of option for you. For one, you don't have to actually delete a record. You just need a flag to say the record is active or not. It should keep your record history.

If that's not an option, try this:
1. Display the current ID on the application.
2. User clicks NEXT
3. select top 1* from yourtable where CurrentID > IDOnYourTable order by ID desc.

This would be slow though, specially if your table is big.

Good Luck!!!

~~ CK
Feb 4 '10 #5

P: 20
Thank you very much for ur reply and suggestion . I did it by attaching flag to the deleted record.
Query sugesstion is very good coz i didnt know that it could be done in sql that way.
Thanks
Feb 5 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.