Results 1 to 5 of 5

Thread: Searching listviews

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2003
    Location
    The MaTRiX
    Posts
    8

    Angry Searching listviews

    Could someone plz tell me how to search a listview for a certain string that has been entered into it. I want to have an inputbox first that asks for the word to search for and then the code searches the listview for that string. If it has been found then a msgbox will come up to saying that it is found.
    Thanks,

    CAIN

  2. #2
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982
    This is what I use to search a listview.

    This only searches the listitem text, It does not search subitems, To search subitems you will have to loop through the listview a cell at a time using 2 loops.

    Code:
    Private Sub FindAnItem(strItem As String, aosp As String)
        Dim itmFound As ListItem   ' FoundItem variable.
        
        Set itmFound = lvwAOS.FindItem(strItem)
        If itmFound Is Nothing Then  ' If no match, inform user and exit.
           MsgBox "Item Not found"
        Else
            ' highlight the item '
            itmFound.EnsureVisible ' Scroll ListView to show found ListItem.
            itmFound.Selected = True   ' Select the ListItem.
        End If
    
    End Sub
    I had to change the above code because the listview had more than one listitem with the same text. If a match was found I had to check the first subitem.
    Code:
    Private Sub FindAnItem(strItem As String, aosp As String)
        Dim itmFound As ListItem   ' FoundItem variable.
        Dim intIndex As Integer
        
        Set itmFound = lvwAOS.FindItem(strItem)
        If itmFound Is Nothing Then  ' If no match, inform user and exit.
           ' Item Not found
        Else
            ' highlight the item '
            Do While (Trim(lvwAOS.SelectedItem.SubItems(1)) <> Trim(aosp) Or Trim(lvwAOS.SelectedItem.Text) <> Trim(strItem))
                intIndex = lvwAOS.SelectedItem.Index
                Set lvwAOS.SelectedItem = lvwAOS.ListItems(intIndex + 1)
            Loop
        End If
        
    End Sub
    hope this helps

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2003
    Location
    The MaTRiX
    Posts
    8
    Thanks for the code but there still is a problem with the first lot of code you gave me. When it searches the listview for a string that i enter it does find it but doesn't display the found item. Also when i enter a string that doesn't exist then it does come up saying item not found. Plz Help
    Thanks,

    CAIN

  4. #4
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982
    If a string doesn't exist it should come up saying 'item not found', shouldn't it? If you want it to display a different message change the code, it is a messagebox command.

    As for not displaying found items check the HideSelection property. It is usually set true by default. It needs to be set to false.

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2003
    Location
    The MaTRiX
    Posts
    8

    Unhappy

    The top code works but i need to be able to search all subitems and then display the result. I am using this code to search:

    Private Sub FindRecord()

    Dim SearchText As String
    Dim itmFound As ListItem

    SearchText = InputBox("Enter the string to search for", "Search")

    Set itmFound = lvInfo.FindItem(SearchText)
    If itmFound Is Nothing Then
    MsgBox "Item not found", vbCritical, "Error"
    Else
    itmFound.EnsureVisible
    itmFound.Selected = True
    End If

    End Sub

    Please help me!!!
    Thanks,

    CAIN

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width