ListView - search on SubItem

Posted on 1998-09-08
Last Modified: 2011-02-15
I need help in trying to carryout a search on subitems in a listview, I can currently search both the main and subitems but when searching the subitem I have to provide the full text even though I specify partial in the finditem function to return a found value. Works fine on the main item, any help would be greatly apreciated.
If possible I would like to use the finditem function of the listview control to search for subitems with only supplying limited text. ie. Serach for Ground and only supply gro to the search function.
Question by:annandap
  • 4
  • 3

Author Comment

ID: 1433752
Edited text of question

Expert Comment

ID: 1433753
Hi annandap,
I basically have a different problem but using the same object which is listview. I am wondering if you know of any method to select a row which have many colums. At the mean time i am only able to select the 1st column. Why?

Your help is really appreciated..and if u don't know what my question is the go to this url:

thank you.
LVL 14

Expert Comment

ID: 1433754
FindItem Method (ListView Control)

Finds and returns a reference to a ListItem object in a ListView control. Syntax

object.FindItem (string, value, index, match)

The FindItem method syntax has these parts:


 Required. An object expression that evaluates to a ListView control.
 Required. A string expression indicating the ListItem object to be found.
 Optional. An integer or constant specifying whether the string will be matched to the ListItem object's Text, Subitems, or Tag property, as described in Settings.
 Optional. An integer or string that uniquely identifies a member of an object collection and specifies the location from which to begin the search. The integer is the value of the Index property; the string is the value of the Key property. If no index is specified, the default is 1.
 Optional. An integer or constant specifying that a match will occur if the item's Text property is the same as the string, as described in Settings.


The settings for value are:


 (Default) Matches the string with a ListItem object's Text property.
 Matches the string with any string in a ListItem object's SubItems property.
 Matches the string with any ListItem object's Tag property.

The settings for match are:


 (Default) An integer or constant specifying that a match will occur if the item's Text property begins with the whole word being searched. Ignored if the criteria is not text.
 An integer or constant specifying that a match will occur if the item's Text property begins with the string being searched. Ignored if the criteria is not text.


If you specify Text as the search criteria, you can use lvwPartial so that a match occurs when the ListItem object's Text property begins with the string you are searching for. For example, to find the ListItem whose text is "Autoexec.bat", use:

'Create a ListItem variable.
Dim itmX As ListItem
'Set the variable to the found item.
Set itmX = ListView1.FindItem("Auto",,,lvwpartial)

FindItem Method Example
This example populates a ListView control with the contents of the Publishers table of the BIBLIO.MDB database. A ComboBox control is also populated with three options for the FindItem method. A CommandButton contains the code for the FindItem method; when you click on the button, you are prompted to enter the string to search for, and the FindItem method searches the ListView control for the string. If the string is found, the control is scrolled using the EnsureVisible method to show the found ListItem object. To try the example, place a ListView, ComboBox, and a CommandButton control on a form and paste the code into the form's Declarations section. Run the example and click on the command button.

Note the example will not run unless you add a reference to the Microsoft DAO 3.0 Object Library by using the References command from the Tools menu.

Private Sub Form_Load()
    ' Create an object variable for the ColumnHeader object.
    Dim clmX As ColumnHeader
    ' Add ColumnHeaders. The width of the columns is the width
    ' of the control divided by the number of ColumnHeader objects.
    Set clmX = ListView1.ColumnHeaders. _
    Add(, , "Company", ListView1.Width / 3)
    Set clmX = ListView1.ColumnHeaders. _
    Add(, , "Address", ListView1.Width / 3)
    Set clmX = ListView1.ColumnHeaders. _
    Add(, , "Phone", ListView1.Width / 3)
    ListView1.BorderStyle = ccFixedSingle  ' Set BorderStyle property.
    ListView1.View = lvwReport  ' Set View property to Report.
    Command1.Caption = "&FindItem"
    ' Label OptionButton controls with FindItem options.
        Option1(0).Caption = "Text"
        Option1(1).Caption = "SubItem"
        Option1(2).Caption = "Tag"
        ListView1.FindItem = 0 ' Set the ListView FindItem property to Text.
    End With
    ' Populate the ListView control with database records.
    ' Create object variables for the Data Access objects.
    Dim myDb As Database, myRs As Recordset
    ' Set the Database to the BIBLIO.MDB database.
    Set myDb = DBEngine.Workspaces(0).OpenDatabase("BIBLIO.MDB")
    ' Set the recordset to the Publishers table.
    Set myRs = myDb.OpenRecordset("Publishers", dbOpenDynaset)
    ' While the record is not the last record, add a ListItem object.
    ' Use the reference to the new object to set properties.
    ' Set the Text property to the Name field (myRS!Name).
    ' Set SubItem(1) to the Address field (myRS!Address).
    ' Set SubItem(7) to the Phone field (myRS!Telephone).

    While Not myRs.EOF
        Dim itmX As ListItem    ' A ListItem variable.
        Dim intCount As Integer    ' A counter variable.
        ' Use the Add method to add a new ListItem and set an object
        ' variable to the new reference. Use the reference to set
        ' properties.
        Set itmX = ListView1.ListItems.Add(, , CStr(myRs!Name))
        intCount = intCount + 1 ' Increment counter for the Tag property.
        itmX.Tag = "ListItem " & intCount  ' Set Tag with counter.

        ' If the Address field is not Null, set SubItem 1 to Address.
        If Not IsNull(myRs!Address) Then
            itmX.SubItems(1) = CStr(myRs!Address) ' Address field.
        End If
        ' If the Phone field is not Null, set SubItem 2 to Phone.
        If Not IsNull(myRs!Telephone) Then
            itmX.SubItems(2) = myRs!Telephone  ' Phone field.
        End If
        myRs.MoveNext    ' Move to next record.
End Sub

Private Sub Command1_Click()
    ' FindItem method.
    'Create an integer variable called intSelectedOption
    ' to store the index of the selected button
    ' Create a string variable called strFindMe. Use the InputBox
    ' to store the string to be found in the variable. Use the
    ' FindItem method to find the string. Option1 is used to
    ' switch the FindItem argument that determines where to look.
    Dim intSelectedOption as Integer
    Dim strFindMe As String
    If Option1(0).Value = True then
        strFindMe = InputBox("Find in " & Option1(0).Caption)
        intSelectedOption = lvwText
    End If
    If Option1(1).Value = True then
        strFindMe = InputBox("Find in " & Option1(1).Caption)
        intSelectedOption = lvwSubItem
    End If
    If Option1(2).Value = True then
        strFindMe = InputBox("Find in " & Option1(2).Caption)
        intSelectedOption = lvwTag
    End If

    ' FindItem method returns a reference to the found item, so
    ' you must create an object variable and set the found item
    ' to it.
    Dim itmFound As ListItem    ' FoundItem variable.
    Set itmFound = ListView1. _
    FindItem(strFindMe, intSelectedOption, , lvwPartial)
    ' If no ListItem is found, then inform user and exit. If a
    ' ListItem is found, scroll the control using the EnsureVisible
    ' method, and select the ListItem.
    If itmFound Is Nothing Then  ' If no match, inform user and exit.
        MsgBox "No match found"
        Exit Sub
         itmFound.EnsureVisible ' Scroll ListView to show found ListItem.
         itmFound.Selected = True    ' Select the ListItem.
        ' Return focus to the control to see selection.
    End If
End Sub

Private Sub ListView1_LostFocus()
    ' After the control loses focus, reset the Selected property
    ' of each ListItem to False.
    Dim i As Integer
    For i = 1 to ListView1.ListItems.Count
        ListView1.ListItems.Item(i).Selected = False
    Next i
End Sub

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 14

Expert Comment

ID: 1433755
You could try also this :
Private Sub Command1_Click()

For i = 1 To TreeView1.Nodes.Count
   If TreeView1.Nodes(i).Text = text1  then
     TreeView1.Nodes(i).Selected = true



Author Comment

ID: 1433756
What you say is straight from the VB Help, if you program the example and test it you will find that it doesn't search on partial text in the second column(sub Item) as I state in the text of my request.
For Roughneck: You can use an API call to select the whole row, see MS Site or look as some of the old VB Questions on this site.

Regards ....

Peter Annandale.
LVL 14

Expert Comment

ID: 1433757
I use this function to find in a listbox, you should try it with listview, it will maybe work?

Private Const LB_FINDSTRING = &H18F

Private Declare Function SendMessageStr Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lparam As String) As Long

Public Function FindListboxItem(ctrl As Control, sStr As String) As Long
   ' *** Find listindex based on a partial code

   On Error Resume Next
   FindListboxItem = SendMessageStr(ctrl.hWnd, LB_FINDSTRING, 0&, (sStr))

End Function


Author Comment

ID: 1433758
I have found a solution by searching the item and subitem using the "like" operator and then assigning the whole item to a variable and feeding the variable into the finditem function.  This works well and doesn't sem to add much more time for the search.
It would seem that you cannot search a subitem of a listbox with partial text, you need to supply the wholtext for it to gain a match (testing I carried out seems to justify this).

I thank you for your comments and time, and am willing to conceed the points to you.
LVL 14

Accepted Solution

waty earned 200 total points
ID: 1433759
Thanks a lot.

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question