For a database (Accessclient, SQL-backend) I'm creating a form with a searchfield. The form holds a listbox lstObjects that displays records (based on a rowsource SQL, not a recordset).
After finding the wanted record it needs to be edited by opening an editform. After editing records the listbox needs to display updated results
The search creates a SQL-statement that searches for the wanted value in all fields in the listbox (recordset), so the result of the search is:
[field1] like value or [field2] like value etc...
I can append this to the rowsource SQL of the listbox. The list is narrowed to the result and user can select/open the wanted record.
However, I need to create a search that narrows the results every time a search is done. So, the results of the first search should be searched to get a second (narrower) set of results and so on.
I would not know how to do this through creating a SQL statement (would become very long/complex/errorsensitive?) so I thought I would solve it like this:
So what happens is that after enering a searchvalue, the current recordset is parked in rstUndo (so that user can step back once to the last result when entering wrong search), a filterstring is built, a new recordset is opened based upon the filtered recordset and the listbox is set to the new recordset.
This can be repeated and the results are narrowed down every search.
The problem is however that I cannot get the listbox to refresh data anymore. If I change the underlying data the changes are never displayed in the listbox, except of course when I reopen the form.
If I "set me.lstObjects.recordset = me.lstObjects.recordset.openrecordset" (from msdn) it doens't work.
Adding dbOpenDynaset and dbSeechanges to the "recordset.openrecordset" statements doesn't help
Seems somewhere my recordsets become snapshots or something like that. Might be logical, but I have never used a recordset to fill a listbox before, so I'm not familiar with all the details of that. I was under the impression that by using DAO the recordsets are always "connected"
After finding the wanted record it needs to be edited by opening an editform. After editing records the listbox needs to display updated results
The search creates a SQL-statement that searches for the wanted value in all fields in the listbox (recordset), so the result of the search is:
[field1] like value or [field2] like value etc...
I can append this to the rowsource SQL of the listbox. The list is narrowed to the result and user can select/open the wanted record.
However, I need to create a search that narrows the results every time a search is done. So, the results of the first search should be searched to get a second (narrower) set of results and so on.
I would not know how to do this through creating a SQL statement (would become very long/complex/errorsensitive?) so I thought I would solve it like this:
Code:
Form-module:
Private rstUndo As DAO.Recordset
Private Sub Form_Load()
Set rstUndo = Me.lstObjects.Recordset
End Sub
Public Sub Txt_Search_AfterUpdate()
Call TestSearch(Me!txtSearch)
End Sub
Public Function TestSearch(varSearchvalue As Variant)
Dim rstOld As DAO.Recordset
Dim fld As DAO.Field
Dim strFilter As String
Set rstOld = Me.lstObjects.Recordset
Set rstUndo = rstOld.OpenRecordset(dbOpenDynaset, dbSeeChanges)
For Each fld In rstOld.FIELDS
'build filterstring like [field1] like value OR [field2] like value OR ... etc
Next fld
rstOld.Filter = strFilter
Set Me.lstObjects.Recordset = rstOld.OpenRecordset(dbOpenDynaset, dbSeeChanges)
rstOld.Close
Set rstOld = Nothing
End Function
So what happens is that after enering a searchvalue, the current recordset is parked in rstUndo (so that user can step back once to the last result when entering wrong search), a filterstring is built, a new recordset is opened based upon the filtered recordset and the listbox is set to the new recordset.
This can be repeated and the results are narrowed down every search.
The problem is however that I cannot get the listbox to refresh data anymore. If I change the underlying data the changes are never displayed in the listbox, except of course when I reopen the form.
If I "set me.lstObjects.recordset = me.lstObjects.recordset.openrecordset" (from msdn) it doens't work.
Adding dbOpenDynaset and dbSeechanges to the "recordset.openrecordset" statements doesn't help
Seems somewhere my recordsets become snapshots or something like that. Might be logical, but I have never used a recordset to fill a listbox before, so I'm not familiar with all the details of that. I was under the impression that by using DAO the recordsets are always "connected"
Last edited: