Search code examples
sqlvbscriptasp-classicrecordset

Recordset Paging isn't Working


Can anyone tell me why my recordset paging isn't working. I don't know a whole lot about this stuff, the parts of it I have in my code I pulled from this Microsoft website link: http://support.microsoft.com/kb/202125

Here is my code:

Dim con, con2, rec, rec2, dsn, dsn2, sql, sql2, rName, sDate, eDate, intPageCount
rName=Request.Form("rn")
sDate=FormatDateTime(Request.Form("sD"), 2)
eDate=FormatDateTime(Request.Form("eD"), 2)

dsn = "Radio"
dsn2 = "InfoLib_SQL"
sql = "SELECT * FROM T_RadioControlLog WHERE RadioName = '"&rName&"' AND CONVERT(date, Date, 101) >= 
       CONVERT(date, '"&sDate&"', 101) AND CONVERT(date, Date, 101) <= CONVERT(date, '"&eDate&"', 101)"

set con=Server.CreateObject("ADODB.Connection")
set rec=Server.CreateObject("ADODB.Recordset")
rec.CursorLocation=3

con.Open dsn, "pw", "pw"
rec.Open sql, con
rec.PageSize=5
intPageCount=rec.PageCount

Select Case Request("Action")
    case "<<"
        intpage =1
    case"<"
        intpage = Request("intpage")-1
        if intpage < 1 then intpage = 1
    case ">"
        intpage = Request("intpage")+1
        if intpage > intPageCount then intpage = intPageCount
    case ">>"
        intpage = intPageCount
    case else
        intpage = 1
end select

If NOT rec.eof Then
Response.write("<table>")
rec.AbsolutePage = intpage
For intRecord = 1 to rec.PageSize
    Response.write("<tr><td>Radio Name</td><td>Date</td><td>Employee Name</td><td>ID#</td></tr>")   
    Response.write("<tr><td>" & rec.fields("RadioName") & "</td>")  
    Response.Write("<td>" & rec.fields("Date") & "</td>")
    set con2=Server.CreateObject("ADODB.Connection")
    set rec2=Server.CreateObject("ADODB.Recordset")
    sql2 = "SELECT EMP_NA FROM EMP_TABLE WHERE ID='"&rec.fields("ID#")&"'"
    con2.Open dsn2, "pw", "pw"
    rec2.Open sql2, con2
    Response.Write("<td>" & rec2.fields("EMP") & "</td>")
    Response.Write("<td>" & rec.fields("ID#")& "</td></tr>")
    rec.movenext
    rec2.close
    con2.close
    set rec2=nothing
    set con2=nothing
If rec.EOF Then Exit For
Next
%>
<tr>
<form name="pageNav" action="results.asp" method="post">
<input type="hidden" name="intpage" value="<%=intpage%>">
<input type="submit" name="Action" value="<<">
<input type="submit" name="Action" value="<">
<input type="submit" name="Action" value=">">
<input type="submit" name="Action" value=">>">
Page: <%=intpage & " of " & intPageCount%>
</tr>
</form>
</table>
<%
Else
Response.write("Sorry, no entries were found.")
End If


rec.close
set rec=nothing
con.close
set con=nothing

The calls to the DB work and entries are definetly returned, I get a small table with 5 entries as my recordset is set to return but when I click the >>, <, >, etc. it doesn't display anything, the page just goes blank. If someone can explain how recordset paging works that would be great.


Solution

  • A couple of comments on your method.

    You Dim intPageCount but not intPage not a big deal but still a personal preference that you should Dim it.

    You have fields from previous pages that you don't seem to save during postback. Specifically I see this section of code

    rName=Request.Form("rn")
    sDate=FormatDateTime(Request.Form("sD"), 2)
    eDate=FormatDateTime(Request.Form("eD"), 2)
    

    but no corresponding section like this

    <form name="pageNav" action="results.asp" method="post">
    <input type="hidden" name="rn" value="<%=rName%>">
    <input type="hidden" name="sD" value="<%=sDate%>">
    <input type="hidden" name="eD" value="<%=eDate%>">
    <input type="hidden" name="intpage" value="<%=intpage%>">
    

    to enable callback for those values on postback.