First of all you must already have your form created with a ListBox in it full of data. I usually use the CheckBox Styled ListBox because it allows me to select multiple elements easily but if you only need to select on item of the list use the standard style.
Below the ListBox create a textbox that is going to be used to do the search. Create a button called next that is going to be used to cycle thru other possible matches to your search. And create another button called Select Search to select all items that match your search.
Double click the StateSearch textbox and add the following code to the Change Procedure
Private Sub StateSearch_Change()
For i = 0 To StateList.ListCount - 1
StringPos = InStr(1, StateList.List(i), StateSearch.Text, vbTextCompare)
If StringPos <> 0 Then
StateList.ListIndex = i
StateList.Selected(StateList.ListIndex) = False
StateSearchIndex = i + 1
i = StateList.ListCount - 1
End If
Next
End Sub
What this does is that every time you change the content of the textbox it iterates thru all the checkbox checking if what you wrote matches any item on the list. If it matches, then the item is selected in the ListBox.
Double Click the Next Button and add the following code to the click Procedure
Private Sub nextButton_Click()When the Next button is clicked it goes to the next matching item in the list. When there are no more item matching it goes back to the first one and starts again.
NO_MORE_STATES = True
For i = StateSearchIndex To StateList.ListCount - 1
StringPos = InStr(1, StateList.List(i), StateSearch.Text, vbTextCompare)
If StringPos <> 0 Then
NO_MORE_STATES = False
StateList.ListIndex = i
StateList.Selected(StateList.ListIndex) = False
StateSearchIndex = i + 1
i = StateList.ListCount - 1
End If
Next
If StateSearchIndex = StateList.ListCount - 1 Or NO_MORE_STATES = True Then
StateSearchIndex = 0
End If
End Sub
Double Click the Select Search button and add the following code to the click Procedure
Private Sub SelectSearchButton_Click()
For i = 0 To StateList.ListCount - 1
StringPos = InStr(1, StateList.List(i), StateSearch.Text, vbTextCompare)
If StringPos <> 0 Then
StateList.ListIndex = i
StateList.Selected(StateList.ListIndex) = True
End If
Next
End Sub
What this does is it checks all matching item of the List with the search Text.
Download the source code here
0 comments:
Post a Comment