Michael Schamberger:
Quote:>then I want to create a new recordset based on rsMain, using a new
>strSQL I would think that:
>set rsNew = rsMain.OpenRecordset(strSQL)
The documentation on recordsets, in particular the .Filter property.
Settings and Return Values
The setting or return value is a String data type that contains the WHERE
clause of an SQL statement without the reserved word WHERE.
Remarks
Use the Filter property to apply a filter to a dynaset-, snapshot-, or
forward-onlytype Recordset object.
You can use the Filter property to restrict the records returned from an
existing object when a new Recordset object is opened based on an existing
Recordset object.
In many cases, it's faster to open a new Recordset object by using an SQL
statement that includes a WHERE clause.
Use the U.S. date format (month-day-year) when you filter fields containing
dates, even if you're not using the U.S. version of the Microsoft Jet
database engine (in which case you must assemble any dates by concatenating
strings, for example, strMonth & "-" & strDay & "-" & strYear). Otherwise,
the data may not be filtered as you expect.
If you set the property to a string concatenated with a non-integer value,
and the system parameters specify a non-U.S. decimal character such as a
comma (for example, strFilter = "PRICE > " & lngPrice, and lngPrice =
125,50), an error occurs when you try to open the next Recordset. This is
because during concatenation, the number will be converted to a string using
your system's default decimal character, and Microsoft Jet SQL only accepts
U.S. decimal characters.
=========
So, it gets set, but only takes affect when you create a new recordset based
on the old one:
rsMain.Filter = strNewSQLWhereClauseOnly
set rsNew = rsMain
Or, the Help file's example is:
This example uses the Filter property to create a new Recordset from an
existing Recordset based on a specified condition. The FilterField function
is required for this procedure to run.
Sub FilterX()
Dim dbsNorthwind As Database
Dim rstOrders As Recordset
Dim intOrders As Integer
Dim strCountry As String
Dim rstOrdersCountry As Recordset
Dim strMessage As String
Set dbsNorthwind = OpenDatabase("Northwind.mdb")
Set rstOrders = dbsNorthwind.OpenRecordset("Orders", _
dbOpenSnapshot)
' Populate the Recordset.
rstOrders.MoveLast
intOrders = rstOrders.RecordCount
' Get user input.
strCountry = Trim(InputBox( _
"Enter a country to filter on:"))
If strCountry <> "" Then
' Open a filtered Recordset object.
Set rstOrdersCountry = _
FilterField(rstOrders, "ShipCountry", strCountry)
With rstOrdersCountry
' Check RecordCount before populating Recordset;
' otherwise, error may result.
If .RecordCount <> 0 Then .MoveLast
' Print number of records for the original
' Recordset object and the filtered Recordset
' object.
strMessage = "Orders in original recordset: " & _
vbCr & intOrders & vbCr & _
"Orders in filtered recordset (Country = '" & _
strCountry & "'): " & vbCr & .RecordCount
MsgBox strMessage
.Close
End With
End If
rstOrders.Close
dbsNorthwind.Close
End Sub
Function FilterField(rstTemp As Recordset, _
strField As String, strFilter As String) As Recordset
' Set a filter on the specified Recordset object and then
' open a new Recordset object.
rstTemp.Filter = strField & " = '" & strFilter & "'"
Set FilterField = rstTemp.OpenRecordset
End Function
Note To see the effects of filtering rstOrders, you must set its Filter
property, and then open a second Recordset object based on rstOrders.
Note When you know the data you want to select, it's usually more
efficient to create a Recordset with an SQL statement. This example shows
how you can create just one Recordset and obtain records from a particular
country.
Sub FilterX2()
Dim dbsNorthwind As Database
Dim rstOrders As Recordset
Set dbsNorthwind = OpenDatabase("Northwind.mdb")
' Open a Recordset object that selects records from a
' table based on the shipping country.
Set rstOrders = _
dbsNorthwind.OpenRecordset("SELECT * " & _
"FROM Orders WHERE ShipCountry = 'USA'", _
dbOpenSnapshot)
rstOrders.Close
dbsNorthwind.Close
End Sub