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.
   Wend
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
   Else
       itmFound.EnsureVisible ' Scroll ListView to show found ListItem.
       itmFound.Selected = True   ' Select the ListItem.
      ' Return focus to the control to see selection.
       ListView1.SetFocus
   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