Article ID: 195222
Article Last Modified on 7/6/2006
Only a single-column name may be specified in criteria. This method does not support multi-column searches.
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
'Create a variable for the Cloned Recordset.
Dim clone_rs As ADODB.Recordset
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
With cn
.ConnectionString = "PROVIDER=SQLOLEDB;" & _
"DATA SOURCE=<server>;" & _
"USER ID=<uid>;" & _
"PASSWORD=<pwd>;" & _
"INITIAL CATALOG=<init_cat>"
.Open
End With
With rs
.CursorLocation = adUseClient
.CursorType = adOpenStatic
.LockType = adLockBatchOptimistic
.ActiveConnection = cn
.Open "select * from authors"
End With
'A clone recordset has some benefits :
' - Very little overhead, it is only an object variable containning a
' reference to the original recordset.
' - It does not require another round trip to the server.
' - It maintains separate but shareable bookmarks with the original.
' - Closing and filtering clones does not affect the original or
' other clones.
'Create a clone of the recordset.
Set clone_rs = Rs.Clone
'Apply a filter to the clone using the criteria passed in.
clone_rs.Filter = "state = 'CA' AND city = 'Oakland'"
If clone_rs.EOF Or clone_rs.BOF Then
'If criteria not found move to EOF; just as ADO's Find
rs.MoveLast
rs.MoveNext
Else
'If found, move the Recordset's bookmark to the same location as the
'clone's bookmark.
rs.Bookmark = clone_rs.Bookmark
End If
clone_rs.Close
Set clone_rs = Nothing
rs.Close
cn.Close
Set rs = Nothing
Set cn = Nothing
End Sub
-or-
Public Sub Multi_Find( _
ByRef oRs As ADODB.Recordset, _
sCriteria As String)
'
'This Sub Routine simulates a Find Method that accepts Multi-Find
'Criteria. It searches columns in a recordset for specific values.
'
'ADO Recordset's Find Method has a limitation of single criteria
'finds.
'For instance:
' ADO Recordset's Find only accepts criteria like the following:
' rs.Find = "state = 'CA'"
'
'It generates an error if multiple criteria are passed to it:
' rs.Find = "state = 'CA' AND city = 'Oakland'"
'
'This Sub Routine has the following syntax:
' Multi_Find oRs, sCriteria
'Where:
' oRs is the ADO Recordset object where the Find is to be done.
' sCriteria is a String in the same format as the Find method
' with the addition of multiple conditions can be provided so
' long as each is joined by an "AND".
'
'Example:
' Multi_Find rs, "state = 'CA' AND city = 'Oakland'"
Dim clone_rs As ADODB.Recordset
Set clone_rs = oRs.Clone
clone_rs.Filter = sCriteria
If clone_rs.EOF Or clone_rs.BOF Then
oRs.MoveLast
oRs.MoveNext
Else
oRs.Bookmark = clone_rs.Bookmark
End If
clone_rs.Close
Set clone_rs = Nothing
End Sub
A String containing a statement that specifies the column name, comparison operator, and value to use in the search.
Dim objConnection As New ADODB.Connection
Dim rs As New ADODB.Recordset
objConnection.ConnectionString = _
"Provider=SQLOLEDB;Data Source=<server>;" & _
"User ID=<uid>;Password=<pwd>;" & _
"Initial Catalog=Northwind"
objConnection.Open
rs.Open "Select * from customers", objConnection, _
adOpenStatic, adLockOptimistic
rs.Find "Customerid = 1 and companyname = 'Hello'"
193871 INFO: Passing ADO Recordsets in Visual Basic Procedures
Keywords: kbdatabase kbprb KB195222