此頁沒有內(nèi)容條目
內(nèi)容

MoveFirst、MoveLast、MoveNext 和 MovePrevious 方法范例

該范例使用 MoveFirst、MoveLast、MoveNext 以及 MovePrevious 方法,按照所提供的命令移動(dòng) Recordset 的記錄指針。運(yùn)行該過程需要 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

   ' 使用客戶端游標(biāo)激活 AbsolutePosition 屬性。

   rstAuthors.CursorLocation = adUseClient

   rstAuthors.Open "authors", strCnn, , , adCmdTable

   ' 顯示當(dāng)前記錄信息并獲得用戶的方法選擇。

   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

      ' 根據(jù)用戶輸入調(diào)用方法。

      MoveAny intCommand, rstAuthors

   Loop

   rstAuthors.Close

End Sub

Public 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 Select

End Sub

VBScript 版本

下面是使用 VBScript 編寫、并用于 Active Server Page (ASP) 的相同范例。如需查看該完整功能范例,請(qǐng)使用與 IIS 一同安裝并位于 C:\InetPub\ASPSamp\AdvWorks 的數(shù)據(jù)源 AdvWorks.mdb,來創(chuàng)建名為 AdvWorks 的系統(tǒng)“數(shù)據(jù)源名稱”(DSN)。這是 Microsoft Access 數(shù)據(jù)庫(kù)文件。請(qǐng)使用查找命令定位文件 Adovbs.inc,并將其放入計(jì)劃使用的目錄中。請(qǐng)將以下代碼剪切并粘貼到記事本或其他文本編輯器中,另存為“MoveOne.asp”。這樣,便可在任何客戶端瀏覽器中查看結(jié)果。

可嘗試在記錄集的上限和下限之外移動(dòng)以查看錯(cuò)誤處理的運(yùn)作。

<!-- #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>

<!-- 在服務(wù)器上創(chuàng)建 Connection 和 Recordset 對(duì)象 -->

<%

 ' 創(chuàng)建并打開 Connection 對(duì)象。

Set OBJdbConnection = Server.CreateObject("ADODB.Connection")

OBJdbConnection.Open "AdvWorks"

' 創(chuàng)建并打開 Recordset 對(duì)象。

Set RsCustomerList = Server.CreateObject("ADODB.Recordset")

RsCustomerList.ActiveConnection = OBJdbConnection

RsCustomerList.CursorType = adOpenKeyset

RsCustomerList.LockType = adLockOptimistic

RsCustomerList.Source = "Customers"

RsCustomerList.Open

' 檢查 Request.Form 集合以查看所記錄的任何移動(dòng)。

If Not IsEmpty(Request.Form("MoveAmount")) Then

' 跟蹤該會(huì)話的移動(dòng)數(shù)目和方向。

   Session("Moves") = Session("Moves") + Request.Form("MoveAmount")

   Clicks = Session("Moves")

' 移動(dòng)到上一個(gè)已知位置。

   RsCustomerList.Move CInt(Clicks)

' 檢查移動(dòng)為 + 還是 - 并進(jìn)行錯(cuò)誤檢查。

      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

' 對(duì) Move Button 單擊組合進(jìn)行錯(cuò)誤檢查。

      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>

<!-- 顯示當(dāng)前記錄數(shù)目和記錄集大小 -->

<% 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 列標(biāo)頭行 -->

<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 數(shù)據(jù) -->

  <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>

<!-- 使用隱含窗體字段將值發(fā)送到服務(wù)器 -->

<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 窗體中設(shè)置值。

   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>