PDA

View Full Version : Trying to apply a Recordset to a Form Filter



tombridges
05-11-2015, 02:07 AM
Perhaps I've got this completely wrong, but I have some queries (SQL) saved in a table. I'm using DLookup on a combobox to get the SQL out of the table, generate a recordset, and then apply the recordset to a form.

Here is an extract of the code:

Private Sub cmdApplyFilter_Click()

Dim sSQLApplyFilter
Dim rs As Recordset
Dim db As DAO.Database

'LOOKUP THE SQL FROM THE CHOSEN ITEM IN THE COMBOBOX
sSQLApplyFilter = DLookup("FilterDetail", "Filters", "ID = lstSavedFilters.Value")
'Debug.Print sSQLApplyFilter

FilterFormName = "Event Form"
Set Filterfrm = Forms(FilterFormName)

Set db = CurrentDb
Set rs = db.OpenRecordset(sSQLApplyFilter)

'PRINT THE NUMBER OF RECORDS
If rs.RecordCount <> 0 Then
rs.MoveLast
rs.MoveFirst
Debug.Print rs.RecordCount & " records returned"
Else
Debug.Print "No records returned"
End If

Filterfrm.Recordset = rs

DoCmd.Close acDefault, "Filters"
Set rs = Nothing
Set db = Nothing


End Sub



at the line Filterfrm.Recordset = rs I get the error:

Run-time error 3251: Operation is not supported for this type of object

Does this mean I can't apply a recordset to a Form?

jonh
05-11-2015, 03:45 AM
Recordset is an object so you need to Set it.

Private Sub cmdApplyFilter_Click()
id = 1
Set Me.Recordset = CurrentDb.OpenRecordset(CurrentDb.OpenRecordset("select filterdetail from filters where id=" & id)(0))
End Sub