Show the 4 last records in a table with asp

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • asdasd10
    New Member
    • Apr 2010
    • 14

    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 :
    Code:
    <%
    path = Server.mappath("../db/ServerData.mdb")
    set con = Server.createobject("ADODB.Connection")
    con.open "driver={Microsoft Access Driver (*.mdb)};DBQ="&path
    set r = con.execute("select * from News")
    Dim i
    i = 1
    r.MoveLast
    do until i = 4 Then
    Response.write (""&r.fields("Name")&"")
    r.MovePrevious
    loop
    conn.close
    conn = Nothing
    %>
  • GazMathias
    Recognized Expert New Member
    • Oct 2008
    • 228

    #2
    You aren't working with a recordset, so you can't move back through it.

    Learn about recordsets here

    Gaz

    Comment

    • jhardman
      Recognized Expert Specialist
      • Jan 2007
      • 3405

      #3
      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

      Comment

      • asdasd10
        New Member
        • Apr 2010
        • 14

        #4
        so how i should write it?

        Comment

        • jhardman
          Recognized Expert Specialist
          • Jan 2007
          • 3405

          #5
          the tutorial at w3schools that Gaz posted is a good place to start. in brief:
          Code:
          <%
          path = Server.mappath("../db/ServerData.mdb")
          set con = Server.createobject("ADODB.Connection")
          con.open "driver={Microsoft Access Driver (*.mdb)};DBQ="&path
          
          set r = server.createobject("ADODB.recordset")
          r.open "select * from News", con, 2,3
          Dim i
          i = 1
          r.MoveLast
          do until i = 4 Then
          Response.write (""&r.fields("Name")&"")
          r.MovePrevious
          loop
          conn.close
          conn = Nothing
          %>
          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

          Comment

          • asdasd10
            New Member
            • Apr 2010
            • 14

            #6
            thanks all i managed what i want to do

            Comment

            Working...