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

Move 方法范例

該范例使用 Move 方法定位基于用戶輸入的記錄指針。

Public Sub MoveX()

   Dim rstAuthors As ADODB.Recordset

   Dim strCnn As String

   Dim varBookmark As Variant

   Dim strCommand As String

   Dim lngMove As Long

   ' 打開 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 "SELECT au_id, au_fname, au_lname, city, state " & _

      "FROM Authors ORDER BY au_lname", strCnn, , , adCmdText

   rstAuthors.MoveFirst

   Do While True

      ' 顯示有關(guān)當(dāng)前記錄的信息并詢問要移動的記錄數(shù)。

      strCommand = InputBox( _

         "Record " & rstAuthors.AbsolutePosition & _

         " of " & rstAuthors.RecordCount & vbCr & _

         "Author: " & rstAuthors!au_fname & _

         " " & rstAuthors!au_lname & vbCr & _

         "Location: " & rstAuthors!City & _

         ", " & rstAuthors!State & vbCr & vbCr & _

         "Enter number of records to Move " & _

         "(positive or negative).")

      If strCommand = "" Then Exit Do

      ' 保存書簽以防 Move 向前或向后移動太遠(yuǎn)。

      varBookmark = rstAuthors.Bookmark

      ' Move 方法需要數(shù)據(jù)類型為長整型的參數(shù)。

      lngMove = CLng(strCommand)

      rstAuthors.Move lngMove

      ' 捕獲 BOF 或 EOF。

      If rstAuthors.BOF Then

         MsgBox "Too far backward! " & _

            "Returning to current record."

         rstAuthors.Bookmark = varBookmark

      End If

      If rstAuthors.EOF Then

         MsgBox "Too far forward! " & _

            "Returning to current record."

         rstAuthors.Bookmark = varBookmark

      End If

   Loop

   rstAuthors.Close

End Sub

VBScript 版本

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

請輸入字母或非整數(shù)查看錯誤處理的運(yùn)作。

<!-- #Include file="ADOVBS.INC" -->

<% Language = VBScript %>

<HTML><HEAD>

<TITLE>ADO Move Methods</TITLE></HEAD>

<BODY>

<FONT FACE="MS SANS SERIF" SIZE=2>

<Center>

<H3>ADO Move Methods</H3>

<%

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

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

OBJdbConnection.Open "AdvWorks"

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

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

RsCustomerList.ActiveConnection = OBJdbConnection

RsCustomerList.CursorType = adOpenKeyset

RsCustomerList.LockType = adLockOptimistic

RsCustomerList.Source = "Customers"

RsCustomerList.Open

 ' 檢查該會話中用戶移動的數(shù)目,以窗體中的數(shù)量作為增量。

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

Clicks = Session("Clicks")

' 移動到上次已知的記錄集位置加上由 Form Post 方法傳遞的數(shù)量。

RsCustomerList.Move CInt(Clicks)

' 出錯處理。

   If RsCustomerList.EOF Then

         Session("Clicks") = RsCustomerList.RecordCount

         Response.Write "This is the Last Record"

         RsCustomerList.MoveLast

      Else If RsCustomerList.BOF Then

         Session("Clicks") = 1

         RsCustomerList.MoveFirst

         Response.Write "This is the First Record"

      End If

   End If

%>

<H3>Current Record Number is <BR>

<% If Session("Clicks") = 0 Then

Session("Clicks") = 1

End If

Response.Write(Session("Clicks") )%> 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 = " >">

<H5>Click Direction Arrows for Previous or Next Record

<BR> Click Move Amount to use Move Method

Enter Number of Records to Move + or - </H5>

<Table>

<Form Method = Post Action="Move.asp" Name=Form>

<TR><TD><Input Type="Button" Name = Move Value="Move Amount "></TD><TD></TD><TD>

<Input Type="Text" Size="4" Name="MoveAmount" Value = 0></TD><TR>

</Form></Table></Center>

</BODY>

<Script Language = "VBScript">

Sub Move_OnClick

' 確認(rèn)輸入的移動值為整型。

If IsNumeric(Document.Form.MoveAmount.Value)Then

   Document.Form.MoveAmount.Value = CInt(Document.Form.MoveAmount.Value)

   Document.Form.Submit

Else

   MsgBox "You Must Enter a Number", ,"ADO-ASP Example"

   Document.Form.MoveAmount.Value = 0

End If

End Sub

Sub cmdDown_OnClick

Document.Form.MoveAmount.Value = -1

Document.Form.Submit

End Sub

Sub cmdUp_OnClick

Document.Form.MoveAmount.Value = 1

Document.Form.Submit

End Sub

</Script>

</HTML>