Hi! I'm basically emulating a split form by having a subform in datasheet view that displays all the records of a table. Clicking on a record in the subform populates the mainform with the record's data, just like in a split form.
Anywho, I have a search/filter box in the header of my main form that filters the subform on the fly. It works great so far, but we want to take it a step further by filtering for records that contain criteria across multiple fields. For instance, assuming the table below:
Typing in "r" will result in all of the records being there:
Typing in "ro" will result in:
Typing in "rock, fire" will result in:
Typing in "rock, fire, a cat" will result in:
Typing in "rock, fire, b cat" will result in empty results since no field contains "b cat".
So, commas would separate criteria and only records that contain all criteria would be displayed. This is what we have for the On Change event for our text box:
How can this code be modified to satisfy our request? Thanks!
Anywho, I have a search/filter box in the header of my main form that filters the subform on the fly. It works great so far, but we want to take it a step further by filtering for records that contain criteria across multiple fields. For instance, assuming the table below:
Code:
[U][B] ID | Field A | Field B | Field C [/B][/U] 1 Cat Dog Bird 2 Earth Rock Fire 3 Rain Wind Water 4 AA Cat BB Fire CC Rock
Code:
[U][B] ID | Field A | Field B | Field C [/B][/U] 1 Cat Dog Bi[B]r[/B]d 2 Ea[B]r[/B]th [B]R[/B]ock Fi[B]r[/B]e 3 [B]R[/B]ain Wind Wate[B]r[/B] 4 AA Cat BB Fi[B]r[/B]e CC [B]R[/B]ock
Code:
[U][B] ID | Field A | Field B | Field C [/B][/U] 2 Earth [B]Ro[/B]ck Fire 4 AA Cat BB Fire CC [B]Ro[/B]ck
Code:
[U][B] ID | Field A | Field B | Field C [/B][/U] 2 Earth [B]Rock[/B] [B]Fire[/B] 4 AA Cat BB [B]Fire[/B] CC [B]Rock[/B]
Code:
[U][B] ID | Field A | Field B | Field C [/B][/U] 4 A[B]A Cat[/B] BB [B]Fire[/B] CC [B]Rock[/B]
Typing in "rock, fire, b cat" will result in empty results since no field contains "b cat".
Code:
[U][B] ID | Field A | Field B | Field C [/B][/U]
Code:
Private Sub txtFilter_Change()
Dim strFilterText As String
strFilterText = txtFilter.Text
txtHiddenFilter.Value = strFilterText
Me.sbfm_Tasks.Form.Filter = "TaskName Like '*" & strFilterText & "*' Or TaskDescription Like '*" & strFilterText & "*'"
Me.sbfm_Tasks.Form.FilterOn = True
'Tests for a trailing space and exits the sub routine at this point
'so as to preserve the trailing space, which would be lost if focus was shifted from Text Box txtFilter
If Len(Me.txtHiddenFilter) <> 0 And InStr(Len(txtHiddenFilter), txtHiddenFilter, " ", vbTextCompare) Then
'Returns the cursor to the the end of the text in Text Box txtFilter,
'and restores trailing space lost when focus is shifted to the list box
Me.txtFilter = strFilterText
Me.txtFilter.SetFocus
Me.txtFilter.SelStart = Me.txtFilter.SelLength
Exit Sub
End If
'Returns the cursor to the the end of the text in Text Box txtFilter
Me.txtFilter.SetFocus
If Not IsNull(Len(strFilterText)) Then
Me.txtFilter.SelStart = Len(strFilterText)
End If
End Sub
Comment