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

   ' 使用客户端游标以允许使用 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

      ' 显示有关当前记录的信息并询问要移动的记录数。

      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 向前或向后移动太远。

      varBookmark = rstAuthors.Bookmark

      ' Move 方法需要数据类型为长整型的参数。

      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 的数据源 AdvWorks.mdb,来创建名为 AdvWorks 的系统“数据源名称”(DSN)。这是 Microsoft Access 数据库文件。请使用查找命令定位文件 Adovbs.inc,并将其放入计划使用的目录中。请将以下代码剪切并粘贴到记事本或其他文本编辑器中,另存为“Move.asp”。这样,便可在任何客户端浏览器中查看结果。

请输入字母或非整数查看错误处理的运作。

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

<%

 ' 创建并打开 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

 ' 检查该会话中用户移动的数目,以窗体中的数量作为增量。

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

Clicks = Session("Clicks")

' 移动到上次已知的记录集位置加上由 Form Post 方法传递的数量。

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 列标头行 -->

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

<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

' 确认输入的移动值为整型。

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>