I am working on a database currently, and have set up search boxes in a form. There may be three or four or five of these boxes, depending on the form and the underlying data. While the boxes are working, they reset the recordset when I go from one box to the next. In other words, the first box filters the data, then when I go to the second box, the data resets to all records. What I need is for the boxes to work successively: each additional criteria entered should narrow down the list. I will post my code here, and if you think it helpful, can do a screen grab of the boxes in the form. Thank you for any help. I have spent way too much time trying to figure this out, and while I have resolved some minor issues, the main issue remains.
First I create a text box, named "txt_search_box", then I add the code to the OnKeyUp event. Subsequent boxes on the same form are named "txt_search_box1" and so on.
Private Sub txt_search_box_KeyUp(KeyCode As Integer, Shift As Integer)
'******************************************************************
'
' Search Box
'
'******************************************************************
' Declare variables
Dim filter_data As String
' Apply filter if text is entered in search box
If Len(txt_search_box.Text) > 0 Then
' Set the text entered in search box equal to the variable defined above
filter_data = txt_search_box.Text
' Build the filter string
' Need to add to filter string for each field you wish to search
Me.Form.Filter = " [fiscalyear] LIKE '*" & filter_data & "*'" _
' Apply filter
Me.Form.FilterOn = True
' Prevent text in search box from being removed after refresh
txt_search_box.SelStart = Len(txt_search_box.Text)
Else
' Clear filter string
Me.Form.Filter = ""
' Remove filter
Me.Form.FilterOn = False
' Set focus
txt_search_box.SetFocus
End If
End Sub
First I create a text box, named "txt_search_box", then I add the code to the OnKeyUp event. Subsequent boxes on the same form are named "txt_search_box1" and so on.
Private Sub txt_search_box_KeyUp(KeyCode As Integer, Shift As Integer)
'******************************************************************
'
' Search Box
'
'******************************************************************
' Declare variables
Dim filter_data As String
' Apply filter if text is entered in search box
If Len(txt_search_box.Text) > 0 Then
' Set the text entered in search box equal to the variable defined above
filter_data = txt_search_box.Text
' Build the filter string
' Need to add to filter string for each field you wish to search
Me.Form.Filter = " [fiscalyear] LIKE '*" & filter_data & "*'" _
' Apply filter
Me.Form.FilterOn = True
' Prevent text in search box from being removed after refresh
txt_search_box.SelStart = Len(txt_search_box.Text)
Else
' Clear filter string
Me.Form.Filter = ""
' Remove filter
Me.Form.FilterOn = False
' Set focus
txt_search_box.SetFocus
End If
End Sub