Sun
ONE Active Server Pages Product Home Page Developer Site Version
 

ContentsPreviousNextIndex



ADO Recordset Object MoveFirst, MoveLast, MoveNext, MovePrevious Methods

These methods move to the first, last, next, or previous record in a specified Recordset object and make that record the current record.

MoveFirst, MoveLast, MoveNext, MovePrevious Methods Syntax
recordset.{MoveFirst | MoveLast | MoveNext | MovePrevious}
MoveFirst, MoveLast, MoveNext, MovePrevious Methods Remarks

Use the MoveFirst method to move the current record position to the first record in the recordset.

Use the MoveLast method to move the current record position to the last record in the recordset. The Recordset object must support bookmarks or backward cursor movement; otherwise, the method call will generate an error.

Use the MoveNext method to move the current record position one record forward (toward the bottom of the recordset). If the last record is the current record and you call the MoveNext method, ADO sets the current record to the position after the last record in the recordset (EOF is True). An attempt to move forward when the ADO Recordset Object BOF, EOF Properties property is already True generates an error.

Use the MovePrevious method to move the current record position one record backward (toward the top of the recordset). The Recordset object must support bookmarks or backward cursor movement; otherwise, the method call will generate an error. If the first record is the current record and you call the MovePrevious method, ADO sets the current record to the position before the first record in the recordset (BOF is True). An attempt to move backward when the ADO Recordset Object BOF, EOF Properties property is already True generates an error. If the Recordset object does not support either bookmarks or backward cursor movement, the MovePrevious method will generate an error.

If the recordset is forward-only and you want to support both forward and backward scrolling, you can use the ADO Recordset Object CacheSize Property to create a record cache that will support backward cursor movement through the ADO Recordset Object Move Method. Because cached records are loaded into memory, you should avoid caching more records than is necessary. You can call the MoveFirst method in a forward-only Recordset object; doing so may cause the provider to re-execute the command that generated the Recordset object.

MoveFirst, MoveLast, MoveNext, MovePrevious Methods Example

This VBScript example uses the MoveFirst, MoveLast, MoveNext, and MovePrevious methods to move the record pointer of a recordset based on the supplied command. The MoveAny function is required for this procedure to run. Try moving beyond the upper or lower limits of the recordset to see error-handling work.

<!-- #Include file="ADOVBS.INC" --> 
<% Language = VBScript %> 
<HTML><HEAD> 
<TITLE>ADO 1.5 MoveNext MovePrevious MoveLast MoveFirst Methods</TITLE></HEAD> 
<BODY> 
<FONT FACE="MS SANS SERIF" SIZE=2> 
<Center> 
<H3>ADO Methods<BR>MoveNext MovePrevious MoveLast MoveFirst</H3> 
<!-- Create Connection and Recordset Objects on Server -->
<% 
'Create and Open Connection Object 
Set OBJdbConnection = Server.CreateObject("ADODB.Connection") 
OBJdbConnection.Open "AdvWorks" 
'Create and Open Recordset Object 
Set RsCustomerList = Server.CreateObject("ADODB.Recordset") 
RsCustomerList.ActiveConnection = OBJdbConnection 
RsCustomerList.CursorType = adOpenKeyset 
RsCustomerList.LockType = adLockOptimistic 
RsCustomerList.Source = "Customers" 
RsCustomerList.Open
' Check Request.Form collection to see if any moves are recorded 
If Not IsEmpty(Request.Form("MoveAmount")) Then 
'Keep track of the number and direction of moves this session 
Session("Moves") = Session("Moves") + Request.Form("MoveAmount") 
Clicks = Session("Moves") 
'Move to last known position 
RsCustomerList.Move CInt(Clicks)
'Check if move is + or - and do error checking 
If CInt(Request.Form("MoveAmount")) = 1 Then 
If RsCustomerList.EOF Then 
Session("Moves") = RsCustomerList.RecordCount 
RsCustomerList.MoveLast 
End If 
RsCustomerList.MoveNext 
End If 
If Request.Form("MoveAmount") < 1 Then 
RsCustomerList.MovePrevious
End If
'Check if First Record or Last Record Command Buttons Clicked 
If Request.Form("MoveLast") = 3 Then 
RsCustomerList.MoveLast 
Session("Moves") = RsCustomerList.RecordCount 
End If 
If Request.Form("MoveFirst") = 2 Then 
RsCustomerList.MoveFirst 
Session("Moves") = 1 
End If 
End If
' Do Error checking for combination of Move Button clicks 
If RsCustomerList.EOF Then 
Session("Moves") = RsCustomerList.RecordCount 
RsCustomerList.MoveLast 
Response.Write "This is the Last Record" 
End If 
If RsCustomerList.BOF Then 
Session("Moves") = 1 
RsCustomerList.MoveFirst 
Response.Write "This is the First Record" 
End If 
%>
<H3>Current Record Number is <BR> 
<!-- Display Current Record Number and Recordset Size --> 
<% If IsEmpty(Session("Moves")) Then 
Session("Moves") = 1 
End If 
%>
<%Response.Write(Session("Moves") )%> of <%=RsCustomerList.RecordCount%></H3> 
<HR>
<Center><TABLE COLSPAN=8 CELLPADDING=5 BORDER=0> 
<!-- BEGIN column header row for Customer Table--> 
<TR><TD ALIGN=CENTER BGCOLOR="#008080"> 
<FONT STYLE="ARIAL NARROW" COLOR="#ffffff" SIZE=1>Company Name</FONT> 
</TD> 
<TD ALIGN=CENTER BGCOLOR="#008080"> 
<FONT STYLE="ARIAL NARROW" COLOR="#ffffff" SIZE=1>Contact Name</FONT> 
</TD> 
<TD ALIGN=CENTER WIDTH=150 BGCOLOR="#008080"> 
<FONT STYLE="ARIAL NARROW" COLOR="#ffffff" SIZE=1>Phone Number</FONT> 
</TD> 
<TD ALIGN=CENTER BGCOLOR="#008080"> 
<FONT STYLE="ARIAL NARROW" COLOR="#ffffff" SIZE=1>City</FONT> 
</TD> 
<TD ALIGN=CENTER BGCOLOR="#008080"> 
<FONT STYLE="ARIAL NARROW" COLOR="#ffffff" SIZE=1>State/Province</FONT> 
</TD></TR>
<!--Display ADO Data from Customer Table--> 
<TR> 
<TD BGCOLOR="f7efde" ALIGN=CENTER> 
<FONT STYLE="ARIAL NARROW" SIZE=1> 
<%= RSCustomerList("CompanyName")%> 
</FONT></TD> 
<TD BGCOLOR="f7efde" ALIGN=CENTER> 
<FONT STYLE="ARIAL NARROW" SIZE=1> 
<%= RScustomerList("ContactLastName") & ", " %> 
<%= RScustomerList("ContactFirstName") %> 
</FONT></TD> 
<TD BGCOLOR="f7efde" ALIGN=CENTER> 
<FONT STYLE="ARIAL NARROW" SIZE=1> 
<%= RScustomerList("PhoneNumber")%> 
</FONT></TD> 
<TD BGCOLOR="f7efde" ALIGN=CENTER> 
<FONT STYLE="ARIAL NARROW" SIZE=1> 
<%= RScustomerList("City")%> 
</FONT></TD> 
<TD BGCOLOR="f7efde" ALIGN=CENTER> 
<FONT STYLE="ARIAL NARROW" SIZE=1> 
<%= RScustomerList("StateOrProvince")%> 
</FONT></TD> 
</TR> </Table></FONT> 
<HR> 
<Input Type = Button Name = cmdDown Value = "< "> 
<Input Type = Button Name = cmdUp Value = " >"> 
<BR> 
<Input Type = Button Name = cmdFirst Value = "First Record">
<Input Type = Button Name = cmdLast Value = "Last Record"> 
<H5>Click Direction Arrows to Use MovePrevious or MoveNext 
<BR> </H5>
<!-- Use Hidden Form Fields to send values to Server --> 
<Form Method = Post Action="MoveOne.asp" Name=Form> 
<Input Type="Hidden" Size="4" Name="MoveAmount" Value = 0> 
<Input Type="Hidden" Size="4" Name="MoveLast" Value = 0> 
<Input Type="Hidden" Size="4" Name="MoveFirst" Value = 0> 
</Form></BODY>
<Script Language = "VBScript">
Sub cmdDown_OnClick 
'Set Values in Form Input Boxes and Submit Form 
Document.Form.MoveAmount.Value = -1 
Document.Form.Submit 
End Sub
Sub cmdUp_OnClick 
Document.Form.MoveAmount.Value = 1 
Document.Form.Submit 
End Sub
Sub cmdFirst_OnClick 
Document.Form.MoveFirst.Value = 2 
Document.Form.Submit 
End Sub
Sub cmdLast_OnClick 
Document.Form.MoveLast.Value = 3 
Document.Form.Submit 
End Sub 
</Script></HTML>


ContentsPreviousNextIndex