473,325 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Go to previous record in sql query

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

5 3916
usr123
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
usr123
20
ODBC call failed error '3146'...
Feb 4 '10 #3
usr123
20
How can i close this thread? i have opened another one for teh same problem in detail...
Feb 4 '10 #4
ck9663
2,878 Expert 2GB
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
usr123
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

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

Similar topics

10
by: george | last post by:
Can anyone help? I query a database and return a result on the column "reference". There might be 7 listings. Each row is displayed in a table, with links through to a detail page. I am working...
2
by: eeh | last post by:
Hi, Suppose I have a table "product" with fields id,name,price. I want to get a resultset of previous and current and next record when the resultset is sorted by id and id=10. The table data is...
2
by: Steve | last post by:
Does any anyone have a procedure for a query where a calculated field returns the previous record's value in another field. For example: A F A Z F M Z The primary key is in random...
1
by: AAVF | last post by:
I have a small table that need to keep a balance of work outstanding. The fields are as follows REC_COUNT (auto-index created record counter) DATE (work date) QTY_REQD (work added on that date)...
9
by: Karl Roes | last post by:
Hi All, I would like some advice on Next / Previous record buttons. I have a main form for the client, and a continuous subform listing client transactions. If I open one of these transactions...
4
by: devi | last post by:
Hi, In oracle I have a LAG function using which I could get the previous value of a field. Do we have anything similar to that in SQL Server or access? Thanks Devi
2
by: MLH | last post by:
Fields in MyTable: PostID PostDate RollQtyXfer RollDenomination RollCount37 RollCount23
3
by: Chris | last post by:
Is it possiable to have a iif function in a query where it looks at the next record. for example if I run the query which lists years and if the results in record 2 equals the result in record 1...
7
by: thread | last post by:
Hi all i need to build progression calculator for a record and for this i need to have the possiblity to get the information for the previous record. is it posible to do it or i will need to use...
16
by: zoeb | last post by:
Hi, I am a complete novice to Access VBA and looking for some help to select a record. I am looking to perform an operation on the previous record - i.e. adding a new blank field, and then...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.