ADO Samples

MoveFirst, MoveLast, MoveNext, and MovePrevious Methods Example (VBScript)

This example uses the MoveFirst, MoveLast, MoveNext, and MovePrevious methods to move the record pointer of a Recordset based on the supplied command.

Cut and paste the following code into Notepad or another text editor, and save it as MoveOne.asp. You can view the result in any browser.

<!-- BeginMoveFirstVBS -->
<%@ Language=VBScript %>
<!--#include file="adovbs.inc"-->
<HTML>
<HEAD>
<TITLE>ADO MoveNext, MovePrevious, MoveLast, MoveFirst Methods</TITLE>

<SCRIPT LANGUAGE="VBScript">
<!--
   Sub cmdDown_OnClick
      'Set Values in Form Input Boxes and Submit Form
      Document.form.MoveAction.Value = "MovePrev"
      Document.Form.Submit
   End Sub

   Sub cmdUp_OnClick
      Document.form.MoveAction.Value = "MoveNext"
      Document.Form.Submit
   End Sub

   Sub cmdFirst_OnClick
      Document.form.MoveAction.Value = "MoveFirst"
      Document.Form.Submit
   End Sub

   Sub cmdLast_OnClick
      Document.form.MoveAction.Value = "MoveLast"
      Document.Form.Submit
   End Sub
//-->
</SCRIPT>
</HEAD>

<body bgcolor="white"> 
<h1>ADO MoveNext, MovePrevious, MoveLast, MoveFirst Methods Example</h1>
<!-- Create Connection and Recordset Objects on Server -->
<%
   sConnStr = "Provider=sqloledb;" & _
              "Data Source=" & Request.ServerVariables("SERVER_NAME") & _
              ";Initial Catalog=Northwind;User Id=sa;Password=;"

   'Create and Open Connection Object
   Set objConn = Server.CreateObject("ADODB.Connection") 
   objConn.Open  sConnStr
   'Create and Open Recordset Object
   Set rsEmployee = Server.CreateObject("ADODB.Recordset")
   rsEmployee.ActiveConnection = objConn
   rsEmployee.CursorType = adOpenKeyset
   rsEmployee.CursorLocation = adUseClient
   rsEmployee.LockType = adLockOptimistic
   rsEmployee.Source = "employees"
   rsEmployee.Open

   rsEmployee.MoveFirst

   If Not IsEmpty(Request.Form("MoveAction")) Then
      strAction = Request.Form("MoveAction")
      varPosition  = Request.Form("Position")
      
      rsEmployee.AbsolutePosition = varPosition
      
      Select Case strAction
      
        Case "MoveNext"
        
         rsEmployee.MoveNext
         If rsEmployee.EOF Then
            rsEmployee.MoveLast
            strMessage = "Can't move beyond the last record."
         End If
        
        Case "MovePrev"
        
         rsEmployee.MovePrevious
         If rsEmployee.BOF Then
            rsEmployee.MoveFirst
            strMessage = "Can't move beyond the first record."
         End If

        Case "MoveLast"
      
         rsEmployee.MoveLast
      
        Case "MoveFirst"
      
         rsEmployee.MoveFirst
      
      End Select
   End If
      
%>

<!-- Display Current Record Number and Recordset Size -->
<h2>Record Number <%=RsEmployee.AbsolutePosition%> of <%=RsEmployee.RecordCount%></H2>
<hr>
<table cellpadding=5 border=0>
<!-- BEGIN column header row for Customer Table-->
<tr>
   <th>Name</th>
   <th>Hire Date</th>
</tr>

<!--Display ADO Data from Customer Table-->
<tr>
  <td><%= rsEmployee("LastName") & ", " %> 
      <%= rsEmployee("FirstName") & " " %></td>
  <td><%= rsEmployee("HireDate")%></td>
</tr> 
<tr>
  <td colspan=2><%=strMessage%></td>
</tr>
</table>

<hr>

<form Method=Post 
      Action="MoveFirstVbs.asp" 
      Name=Form>
<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">

<H3>Click Direction Arrows to Use MovePrevious or MoveNext</H3>

<!-- Use Hidden Form Fields to send values to Server -->

<input Type="Hidden" Size="4" Name="MoveAction" Value="Move">
<input Type="Hidden" Size="4" Name="Position" Value="<%= rsEmployee.AbsolutePosition %>">
</form>

<HR>
</BODY>
</HTML>
<!-- EndMoveFirstVBS -->

See Also

MoveFirst, MoveLast, MoveNext, and MovePrevious Methods | Recordset Object