MoveFirst、MoveLast、MoveNext 和 MovePrevious 方法范例

该范例使用 MoveFirstMoveLastMoveNext 以及 MovePrevious 方法,按照所提供的命令移动 Recordset 的记录指针。运行该过程需要 MoveAny 过程。

Public Sub MoveFirstX()   Dim rstAuthors As ADODB.Recordset
   Dim strCnn As String
   Dim strMessage As String
   Dim intCommand As Integer   ' 打开 Authors 表的记录集。
      strCnn = "Provider=sqloledb;" & _
      "Data Source=srv;Initial Catalog=pubs;User Id=sa;Password=; "
   Set rstAuthors = New ADODB.Recordset
   rstAuthors.CursorType = adOpenStatic
   ' 使用客户端游标激活 AbsolutePosition 属性。
   rstAuthors.CursorLocation = adUseClient
   rstAuthors.Open "authors", strCnn, , , adCmdTable   ' 显示当前记录信息并获得用户的方法选择。
   Do While True      strMessage = "Name: " & rstAuthors!au_fName & " " & _
         rstAuthors!au_lName & vbCr & "Record " & _
         rstAuthors.AbsolutePosition & " of " & _
         rstAuthors.RecordCount & vbCr & vbCr & _
         "[1 - MoveFirst, 2 - MoveLast, " & vbCr & _
         "3 - MoveNext, 4 - MovePrevious]"
      intCommand = Val(Left(InputBox(strMessage), 1))
      If intCommand < 1 Or intCommand > 4 Then Exit Do      ' 根据用户输入调用方法。
      MoveAny intCommand, rstAuthors
   Loop
   rstAuthors.CloseEnd SubPublic Sub MoveAny(intChoice As Integer, _
   rstTemp As Recordset)   ' 使用指定方法捕获 BOF 和 EOF。
   Select Case intChoice
      Case 1
         rstTemp.MoveFirst
      Case 2
         rstTemp.MoveLast
      Case 3
         rstTemp.MoveNext
         If rstTemp.EOF Then
            MsgBox "Already at end of recordset!"
            rstTemp.MoveLast
         End If
      Case 4
         rstTemp.MovePrevious
         If rstTemp.BOF Then
            MsgBox "Already at beginning of recordset!"
            rstTemp.MoveFirst
         End If
   End SelectEnd Sub

VBScript 版本

下面是使用 VBScript 编写、并用于 Active Server Page (ASP) 的相同范例。如需查看该完整功能范例,请使用与 IIS 一同安装并位于 C:\InetPub\ASPSamp\AdvWorks 的数据源 AdvWorks.mdb,来创建名为 AdvWorks 的系统“数据源名称”(DSN)。这是 Microsoft Access 数据库文件。请使用查找命令定位文件 Adovbs.inc,并将其放入计划使用的目录中。请将以下代码剪切并粘贴到记事本或其他文本编辑器中,另存为“MoveOne.asp”。这样,便可在任何客户端浏览器中查看结果。

可尝试在记录集的上限和下限之外移动以查看错误处理的运作。

<!-- #Include file="ADOVBS.INC" -->
<% Language = VBScript %>
<HTML><HEAD>
<TITLE>ADO 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>
<!-- 在服务器上创建 Connection 和 Recordset 对象 -->
<%
 ' 创建并打开 Connection 对象。
Set OBJdbConnection = Server.CreateObject("ADODB.Connection") 
OBJdbConnection.Open "AdvWorks" 
' 创建并打开 Recordset 对象。
Set RsCustomerList = Server.CreateObject("ADODB.Recordset")
RsCustomerList.ActiveConnection = OBJdbConnection
RsCustomerList.CursorType = adOpenKeyset
RsCustomerList.LockType = adLockOptimistic
RsCustomerList.Source = "Customers"RsCustomerList.Open' 检查 Request.Form 集合以查看所记录的任何移动。If Not IsEmpty(Request.Form("MoveAmount")) Then
' 跟踪该会话的移动数目和方向。
   
   Session("Moves") = Session("Moves") + Request.Form("MoveAmount")
   
   Clicks = Session("Moves")
' 移动到上一个已知位置。
   RsCustomerList.Move CInt(Clicks)
' 检查移动为 + 还是 - 并进行错误检查。
      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
' 检查有无单击 First Record 或 Last Record 命令按钮。
      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
' 对 Move Button 单击组合进行错误检查。
      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>
<!-- 显示当前记录数目和记录集大小 -->
<% 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><!-- Customer 表的 BEGIN 列标头行 --><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><!-- 显示 Customer 表的 ADO 数据 -->  <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><!-- 使用隐含窗体字段将值发送到服务器 --><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
' 在 Input Boxes 窗体和 Submit 窗体中设置值。
   Document.Form.MoveAmount.Value = -1
   Document.Form.Submit
End SubSub cmdUp_OnClick   Document.Form.MoveAmount.Value = 1
   Document.Form.SubmitEnd SubSub cmdFirst_OnClick   Document.Form.MoveFirst.Value = 2
   Document.Form.SubmitEnd SubSub cmdLast_OnClick   Document.Form.MoveLast.Value = 3
   Document.Form.SubmitEnd Sub
</Script></HTML>
www.holmesian.org