MoveFirst、MoveLast、MoveNext、MovePrevious メソッドの例 (VBScript)

この例では、与えられたコマンドに基づいて Recordset のレコード ポインタを移動するのに、MoveFirstMoveLastMoveNext、および MovePrevious メソッドを使います。このプロシージャを実行するには、MoveAny プロシージャが必要です。

次の例は、Active Server Pages (ASP) で使用してください。この例を完全に機能させて表示するには、SDK と共にインストールされるデータ ソース AdvWorks.mdb が、C:\mssdk\samples\dataaccess\rds に置かれている必要があります。これは、Microsoft Access のデータベース ファイルです。

検索機能を使ってファイル Adovbs.inc を検索し、使用するディレクトリに Adovbs.inc を置きます。次のコードをコピーして、メモ帳または別のテキスト エディタに貼り付け、「MoveOne.asp」という名前で保存します。結果は、任意のブラウザで表示できます。

レコードセットの上限または下限を越えて移動しようとすると、エラー処理の動作を確認できます。

<%@ Language=VBScript %>
<!-- #Include file="ADOVBS.INC" -->
<HTML><HEAD>
<TITLE>ADO MoveNext, MovePrevious, MoveLast, MoveFirst Methods</TITLE>
<STYLE>
<!--
BODY {
    font-family: "MS SANS SERIF",sans-serif;
     }
.thead {
    background-color: #008080; 
    font-family: 'Arial Narrow','Arial',sans-serif; 
    font-size: x-small;
    color: white;
    }
.tbody { 
    text-align: center;
    background-color: #f7efde;
    font-family: 'Arial Narrow','Arial',sans-serif; 
    font-size: x-small;
     }
.tmsg {
    color: red;
    text-align: center;
    }
-->
</STYLE>
</HEAD>

<BODY> 
<H3>ADO Methods<BR>MoveNext, MovePrevious, MoveLast, MoveFirst</H3>
<!-- Create Connection and Recordset Objects on Server -->
<%
src = "c:\mssdk\samples\dataaccess\rds\advworks.mdb"
sConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & src

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

RsCustomerList.MoveFirst

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

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

<H3>Current Record Number is <BR>
<!-- Display Current Record Number and Recordset Size -->
<%=RsCustomerList.AbsolutePosition%> of <%=RsCustomerList.RecordCount%></H3>
<HR>
<TABLE COLSPAN=8 CELLPADDING=5 BORDER=0>
<!-- BEGIN column header row for Customer Table-->
<TR CLASS=thead>
    <TD>Company Name</TD>
    <TD>Contact Name</TD>
    <TD>Phone Number</TD>
    <TD>City</TD>
    <TD>State/Province</TD>
</TR>

<!--Display ADO Data from Customer Table-->
<TR CLASS=tbody>
  <TD> <%= RSCustomerList("CompanyName")%> </TD>
  <TD> <%= RScustomerList("ContactLastName") & ", " %> 
       <%= RScustomerList("ContactFirstName") %> </TD>
  <TD> <%= RScustomerList("PhoneNumber")%> </TD>
  <TD> <%= RScustomerList("City")%> </TD>
  <TD> <%= RScustomerList("StateOrProvince")%> </TD>
</TR> 
<TR CLASS=tmsg>
  <TD COLSPAN=5><%=strMessage%></TD>
</TR>
</TABLE>

<HR>
<Input Type=Button Name=cmdDown  Value="&lt;  ">
<Input Type=Button Name=cmdUp Value="  &gt;">
<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</H5>

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

<Form Method=Post 
      Action="<%=Request.ServerVariables("SCRIPT_NAME")%>" 
      Name=Form>
<Input Type="Hidden" Size="4" Name="MoveAction" Value="Move">
<Input Type="Hidden" Size="4" Name="Position" Value="<%= RsCustomerList.AbsolutePosition %>">
</Form>

<HR>
</BODY>

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