Eljefegeneo
Still trying to learn
- Local time
- Today, 01:40
- Joined
- Jan 10, 2011
- Messages
- 904
I want to use a variation of the popular "search as you type combo box" using the following VBA. What I cannot figure out to do is populate the combo box selection criteria, as in the case below, if I want to have a cbo1, a first combo box, populate the field in the second combo box, cboFilter. For example, I have five fields that I want to use for search criteria. Surname, Organization, City, State and Zip. I typed a list in cbo1 that included all of the five above, but I can't figure out how to select the rowsource for the second box, cboFilter, so if I select Surname in cbo1, then I can start typing in cboFilter to search for a surname. Any suggestions?
Below uses the fieldPrivate Sub cboFilter_Change()
' If the combo box is cleared, clear the form filter.
If Nz(Me.cboFilter.Text) = "" Then
Me.Form.Filter = ""
Me.FilterOn = False
' If a combo box item is selected, filter for an exact match.
' Use the ListIndex property to check if the value is an item in the list.
ElseIf Me.cboFilter.ListIndex <> -1 Then
Me.Form.Filter = "[fieldName] = '" & _
Replace(Me.cboFilter.Text, "'", "''") & "'"
Me.FilterOn = True
' If a partial value is typed, filter for a partial fieldName match.
Else
Me.Form.Filter = "[fieldname] Like '*" & _
Replace(Me.cboFilter.Text, "'", "''") & "*'"
Me.FilterOn = True
End If
' Move the cursor to the end of the combo box.
Me.cboFilter.SetFocus
Me.cboFilter.SelStart = Len(Me.cboFilter.Text)
End Sub
Below uses the fieldPrivate Sub cboFilter_Change()
' If the combo box is cleared, clear the form filter.
If Nz(Me.cboFilter.Text) = "" Then
Me.Form.Filter = ""
Me.FilterOn = False
' If a combo box item is selected, filter for an exact match.
' Use the ListIndex property to check if the value is an item in the list.
ElseIf Me.cboFilter.ListIndex <> -1 Then
Me.Form.Filter = "[fieldName] = '" & _
Replace(Me.cboFilter.Text, "'", "''") & "'"
Me.FilterOn = True
' If a partial value is typed, filter for a partial fieldName match.
Else
Me.Form.Filter = "[fieldname] Like '*" & _
Replace(Me.cboFilter.Text, "'", "''") & "*'"
Me.FilterOn = True
End If
' Move the cursor to the end of the combo box.
Me.cboFilter.SetFocus
Me.cboFilter.SelStart = Len(Me.cboFilter.Text)
End Sub