Select listbox item

spinkung

Registered User.
Local time
Today, 00:36
Joined
Dec 4, 2006
Messages
267
Hi

i have a textbox which i want to use to search for entries in a listbox. There are 2 buttons. Search Up & Search Down.

When the user searches down it should find the first entry with matching text (InStr). If the search down button is clicked again it should find the next entry. This is working BUT....

after clicking the down arrow for the second time the listindex number is still that of the previously selected item so the search fails. It's like it's selected the item but not updated the listindex position?? If i then manually click the entry in the listbox and click search down, the next matching entry is found.


Code:
Dim idxSearch, idxOld, value As Variant
Dim searchVal As String

On Error GoTo no_search_text:
searchVal = UCase(Me.txt_search.value)

idxSearch = Me.list_invoices.ListIndex
idxOld = Me.list_invoices.ListIndex

' loop through the listbox
    For idxSearch = idxSearch + 1 To Me.list_invoices.ListCount - 1
       value = UCase(list_invoices.Column(0, idxSearch))
       ' search ofr the text string in each list entry
        If InStr(value, searchVal) Then
        ' if a match is found select entry
          Me.list_invoices.Selected(idxSearch) = True
          ' unselect previously selected item. This is only applicable to multi select listboxes
          If idxSearch <> 0 Then
            Me.list_invoices.Selected(idxOld) = False
          End If
          Exit Sub
        End If
    Next idxSearch
    Exit Sub

no_search_text:
    MsgBox "Nothing to find"
    Exit Sub

The list box is valuelist, multiselect (extended)

is there a command to move the listindex position in vba or something else i need to think about?

thanks in advance
 
http://www.access-programmers.co.uk/forums/showthread.php?t=246880
Here is a function posted yesterday. Pass in the listbox name and the number.
You can modify it for a string.
Note the solution - the number must be re-assigned back to the list box. Otherwise, the next refresh or listbox reference appears to go back to the old value.

Basically, passing the listbox object pointer into a variable.
 

Users who are viewing this thread

Back
Top Bottom