A request was made for an example script which illustrates the use of enabling a user with the ability to page through recordsets. Here is an axample which moves data from a database into a client-side array and provides a control panel which allows the user to page through the recordset, one record at a time. By putting the data into an array, excessive trips back to the server are minimized.
There is also an added feature which allows the developer to create a libray of predefined reports. The user can select the desired report from a droplist. Each report selection triggers a new query which submits to the server by having the page call itself.
'------------------------------------------------------------------ 'Page calls back to itself so get name '------------------------------------------------------------------
'------------------------------------------------------------------ 'Connect to database and define tablename and key field '------------------------------------------------------------------
'------------------------------------------------------------------ 'Set first query equal to default selection for reports '------------------------------------------------------------------
if Request("report") = "" then recordID = "<= 10250" else recordID= Request("report") end if
'------------------------------------------------------------------ 'Get the recordset and move into client-side array '------------------------------------------------------------------
If numrecords <= 0 then response.redirect "error.asp" end if
'------------------------------------------------------------------ 'Use server to write out script commands '------------------------------------------------------------------
'------------------------------------------------------------------ 'get field names from table '------------------------------------------------------------------
For I = 0 to rs.Fields.Count - 1 redim preserve ARR(I) ARR(I) = rs.Fields(I).Name If I=0 then myarray=myarray & rs.Fields(I).Name else myarray=myarray & " " & rs.Fields(I).Name end if next
'------------------------------------------------------------------ 'Now lets grab all the records using rs.getrows 'Close and cleanup recordset as data is now in an array '------------------------------------------------------------------
FOR rowcounter= 0 TO numrows myrsarray="" redim preserve ARRrs(rowcounter) response.write("ARRrs(" & rowcounter & ") = " & chr(34)) FOR colcounter=0 to numcols thisfield=alldata(colcounter,rowcounter) if isnull(thisfield) then thisfield=shownull end if if trim(thisfield)="" then thisfield=showblank end if If colcounter=0 then myrsarray=myrsarray & thisfield else myrsarray=myrsarray & " " & thisfield end if
NEXT response.write(myrsarray) response.write(chr(34) & vbcrlf) NEXT
response.write("</script>" & vbcrlf)
End Sub
'------------------------------------------------------------------ 'Begin scripts to handle selection of record for viewing 'default is record 0 and button 1 (first) ' 'Case 2 and 3 relative positioning handled by "select case" ' 'Case 1 and 4 are handled directly from button tags since 'their boundary conditions are known '------------------------------------------------------------------
%>
<Script Language="VBScript"><!--
call getrecord(0,1)
Function getrecord(arraynumber,button)
on error resume next
Select case button
Case "2" ' Previous record If ISNull(arraynumber) then arraynumber = 0 If arraynumber < 0 then arraynumber = numrows end if
Case "3" 'Next record If ISNull(arraynumber) then arraynumber = 0 if arraynumber > numrows then arraynumber = 0 end if end Select
ARR=split(ARR)
for i = 0 to fieldnamecount document.myform.fieldname(i).value = ARR(i) next
row= ARRrs(arraynumber) r=split(row)
for i = 0 to numcols document.myform.fieldvalue(i).value = r(i) next
document.myform.arraynumber.value = arraynumber End function
--></Script>
</HEAD> <%
'------------------------------------------------------------------ 'Create the control panel and buttons to scroll through data 'and create droplist for selectable reports '------------------------------------------------------------------
%> <HTML> <TITLE>Report Script to View Records</TITLE> <HEAD> <BODY bgcolor="gray">
'------------------------------------------------------------------ 'Set up the report selection list as an array value 'Dimension the array = number of defined reports -1 'because arrays are zero based '------------------------------------------------------------------
'------------------------------------------------------------------ 'The selected value is set to your preference of defined reports 'the remainder of the list is generated from the array 'First condition checks for initial call to script '------------------------------------------------------------------
if request("Report")="" then%> <OPTION value="<=10250" selected>first 3 records</OPTION> <% for i = 1 to Ubound(ReportARR) ReportARRvalues=split(ReportARR(i),",")%> <OPTION value="<%=ReportARRvalues(0)%>"><%=ReportARRvalues(1)%></OPTION> <%next end if
'------------------------------------------------------------------ 'Subsequent calls for reports are used to select display value '------------------------------------------------------------------
if request("Report")<>"" then for i = 0 to Ubound(ReportARR) ReportARRvalues=split(ReportARR(i),",") if request("Report")= ReportARRvalues(0) then%> <OPTION value="<%=ReportARRvalues(0)%>" selected><%=ReportARRvalues(1)%></OPTION> <%else %> <OPTION value="<%=ReportARRvalues(0)%>"><%=ReportARRvalues(1)%></OPTION> <%end if next end if %>
'------------------------------------------------------------------ 'Setup form to view report output '------------------------------------------------------------------