會(huì)員登錄 - 用戶注冊(cè) - 網(wǎng)站地圖 Office中國(guó)(office-cn.net),專業(yè)Office論壇
當(dāng)前位置:主頁 > 技巧 > Access技巧 > DAO/ADO/ADP > 正文

在Recordset對(duì)象中查詢記錄的方法

時(shí)間:2004-01-08 08:31 來源:本站原創(chuàng) 作者:zhengjia… 閱讀:
    無論是DAO還是ADO都有兩種從Recordset對(duì)象中查詢記錄的方法:Find方法和Seek方法。在這兩種方法中可以讓你指定條件進(jìn)行查詢與其相應(yīng)的記錄, 一般而言,在相同條件下,Seek方法提供了比Find方法更好的性能,因?yàn)?/SPAN>Seek方法是基于索引的。因?yàn)檫@個(gè)原因基本提供者必須支持 Recordset 對(duì)象上的索引,可以用Supports(adSeek) 方法確定基本提供者是否支持Seek,用Supports(adIndex) 方法確定提供者是否支持索引。(例如,OLE DB Provider for Microsoft Jet支持SeekIndex。),請(qǐng)將Seek方法和Index屬性結(jié)合使用。如果Seek沒有找到所需的行,將不會(huì)產(chǎn)生錯(cuò)誤,該行將被放在Recordset 的結(jié)尾處。執(zhí)行此方法前,請(qǐng)先將Index 屬性設(shè)置為所需的索引。此方法只受服務(wù)器端游標(biāo)支持。如果 Recordset對(duì)象的CursorLocation屬性值為adUseClient,將不支持 Seek。只有當(dāng) CommandTypeEnum值為adCmdTableDirect 時(shí)打開Recordset對(duì)象,才可以使用此方法。 

ADO Find方法 

DAO包含了四個(gè)“Find”方法:FindFirst,FindLast,FindNextFindPrevious. 

DAO方法   ADO Find 方法 

下面的一個(gè)例子示范了如何用ADO Find方法查詢記錄:

Sub FindRecord(strDBPath As String, _

               strTable As String, _

               strCriteria As String, _

               strDisplayField As String)

    ' This procedure finds a record in the specified table by

    ' using the specified criteria.

    ' For example, to use this procedure to find records

    ' in the Customers table in the Northwind database

    ' that have "USA" in the Country field, you can

    ' use a line of code like this:

    ' FindRecord _

    '    "c:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb", _

    '    "Customers", "Country='USA'", "CustomerID" 

    Dim cnn As ADODB.Connection

    Dim rst As ADODB.Recordset   

    ' Open the Connection object.

    Set cnn = New ADODB.Connection

    With cnn

        .Provider = "Microsoft.Jet.OLEDB.4.0"

        .Open strDBPath

    End With   

    Set rst = New ADODB.Recordset

    With rst

        ' Open the table by using a scrolling

        ' Recordset object.

        .Open Source:=strTable, _

              ActiveConnection:=cnn, _

              CursorType:=adOpenKeyset, _

              LockType:=adLockOptimistic 

        ' Find the first record that meets the criteria.

        .Find Criteria:=strCriteria, SearchDirection:=adSearchForward       

        ' Make sure record was found (not at end of file).

        If Not .EOF Then

            ' Print the first record and all remaining

            ' records that meet the criteria.

            Do While Not .EOF

                Debug.Print .Fields(strDisplayField).Value

                ' Skip the current record and find next match.

                .Find Criteria:=strCriteria, SkipRecords:=1

            Loop

        Else

            MsgBox "Record not found"

        End If

        ' Close the Recordset object.

        .Close

    End With

     ' Close connection and destroy object variables.

    cnn.Close   

    Set rst = Nothing

    Set cnn = Nothing   

End Sub 

例如,用用這個(gè)過程查詢“羅期文商貿(mào)”示例數(shù)據(jù)庫中“客戶”表的“國(guó)家”等于USA的記錄,可以使用下面的代碼:

FindRecord “c:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb”, _

   “Customers”, “Country=’USA’”, ”CustomerID”

 (譯者注:如果你安裝的是簡(jiǎn)體中文版要將相應(yīng)的字段名改成中文) 

ADO Seek方法 

因?yàn)?/SPAN>ADO Seek方法使用Index,最好是在用這個(gè)方法之前指定一個(gè)索引,可是,如果你沒有指定索引,Jet database engine將用主鍵。

如果你需要從多個(gè)字段中指定值做為搜索條件,可以用VBA中的Array函數(shù)傳遞這些值到參數(shù)KeyValues中去。如果你只需要從一個(gè)字段中指定值做為搜索條件,則不需要用Array函數(shù)傳遞。

Find方法一樣,你可以用BOF或者EOF屬性測(cè)試是否查詢到記錄。 

下面的一個(gè)例子顯示了如何用ADO Seek方法查詢記錄: 

Sub SeekRecord(strDBPath As String, _

               strIndex As String, _

               strTable As String, _

               varKeyValues As Variant, _

               strDisplayField As String)

     ' This procedure finds a record by using

    ' the specified index and key values.

    ' For example, to use the PrimaryKey index to

    ' find records in the Order Details table in the

    ' Northwind database where the OrderID field is

    ' 10255 and ProductID is 16, and then display the

    ' value in the Quantity field, you can use a line

    ' of code like this:

    ' SeekRecord _

    '   "c:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb", _

    '   "PrimaryKey", "Order Details", Array(10255, 16), "Quantity"

    Dim cnn As ADODB.Connection

    Dim rst As ADODB.Recordset  

    ' Open the Connection object.

    Set cnn = New ADODB.Connection

    With cnn

        .Provider = "Microsoft.Jet.OLEDB.4.0"       

        .Open strDBPath

    End With

    Set rst = New ADODB.Recordset

    With rst

        ' Select the index used to order the

        ' data in the recordset.

        .Index = strIndex   

        ' Open the table by using a scrolling

        ' Recordset object.

        .Open Source:=strTable, _

              ActiveConnection:=cnn, _

              CursorType:=adOpenKeyset, _

              LockType:=adLockOptimistic, _

              Options:=adCmdTableDirect   

        ' Find the order where OrderId = 10255 and

        ' ProductId = 16.

        .Seek KeyValues:=varKeyValues, SeekOption:=adSeekFirstEQ   

        ' If a match is found, print the value of

        ' the specified field.

        If Not .EOF Then

            Debug.Print .Fields(strDisplayField).Value

        End If 

        ' Close the Recordset object.

        .Close

    End With

   ' Close connection and destroy object variables.

    cnn.Close   

    Set rst = Nothing

    Set cnn = Nothing   

End Sub 

例如,用主鍵索引查詢“羅期文商貿(mào)”示例數(shù)據(jù)庫中“訂單明細(xì)”表的“訂單編號(hào)”等于10255并且產(chǎn)品編號(hào)等于16數(shù)量的值,可以使用下面的代碼:

SeekRecord “c:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb”, _

   “PrimaryKey”, “Order Details”, Array(10255,16), ”Quantity”

 (譯者注:如果你安裝的是簡(jiǎn)體中文版要將示例中引用的相應(yīng)的字段名改成中文)

(責(zé)任編輯:admin)

頂一下
(0)
0%
踩一下
(0)
0%
發(fā)表評(píng)論
請(qǐng)自覺遵守互聯(lián)網(wǎng)相關(guān)的政策法規(guī),嚴(yán)禁發(fā)布色情、暴力、反動(dòng)的言論。
評(píng)價(jià):