468,483 Members | 2,688 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,483 developers. It's quick & easy.

Show the 4 last records in a table with asp

i tired do it alone but its gives me an eror like that:
"r object cant move back"

here is my code :
Expand|Select|Wrap|Line Numbers
  1. <%
  2. path = Server.mappath("../db/ServerData.mdb")
  3. set con = Server.createobject("ADODB.Connection")
  4. con.open "driver={Microsoft Access Driver (*.mdb)};DBQ="&path
  5. set r = con.execute("select * from News")
  6. Dim i
  7. i = 1
  8. r.MoveLast
  9. do until i = 4 Then
  10. Response.write (""&r.fields("Name")&"")
  11. r.MovePrevious
  12. loop
  13. conn.close
  14. conn = Nothing
  15. %>
Apr 5 '10 #1
5 1766
GazMathias
200 Expert 100+
You aren't working with a recordset, so you can't move back through it.

Learn about recordsets here

Gaz
Apr 5 '10 #2
jhardman
3,405 Expert 2GB
Even when you start working with recordsets, the default cursor is forward only. Either query in such a way that you start with the newest, or specify a cursor that can move both ways.

Jared
Apr 5 '10 #3
so how i should write it?
Apr 5 '10 #4
jhardman
3,405 Expert 2GB
the tutorial at w3schools that Gaz posted is a good place to start. in brief:
Expand|Select|Wrap|Line Numbers
  1. <%
  2. path = Server.mappath("../db/ServerData.mdb")
  3. set con = Server.createobject("ADODB.Connection")
  4. con.open "driver={Microsoft Access Driver (*.mdb)};DBQ="&path
  5.  
  6. set r = server.createobject("ADODB.recordset")
  7. r.open "select * from News", con, 2,3
  8. Dim i
  9. i = 1
  10. r.MoveLast
  11. do until i = 4 Then
  12. Response.write (""&r.fields("Name")&"")
  13. r.MovePrevious
  14. loop
  15. conn.close
  16. conn = Nothing
  17. %>
the 2,3 on the r.open line is what specifies which type of cursor you are using. This actually gives you one that can make edits to the db, and that's probably not what you want. the w3schools site gives more details, and some examples of what constants to use there.

Jared
Apr 5 '10 #5
thanks all i managed what i want to do
Apr 5 '10 #6

Post your reply

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

Similar topics

3 posts views Thread by Ricardo | last post: by
reply views Thread by Tom Kaminski [MVP] | last post: by
15 posts views Thread by Joachim | last post: by
6 posts views Thread by Christo | last post: by
1 post views Thread by kmladenovski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.