GetFirstVisible Method Example

This example populates a ListView control with the contents of the Publishers table in the Biblio.mdb database. When you click on the CommandButton control, the text of the first visible item is displayed. Click on the column headers to change the SortKey property and click the CommandButton again. To try the example, place a ListView and a CommandButton control on a form and paste the code into the form's Declarations section.

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

Private Sub Command1_Click()
   ' Create a ListItem variable and set the variable to the object
   ' returned by the GetFirstVisible method. Use the reference to
   ' display the text of the ListItem.
   Dim itmX As ListItem
   Set itmX = ListView1.GetFirstVisible
   MsgBox itmX.Text
End Sub

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.
   
   ' 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)
   
   ' Create a variable to add ListItem objects.
   Dim itmX As ListItem

   ' While the record is not the last record, add a ListItem object.
   ' Use the Name field for the ListItem object's text.
   ' Use the Address field for the ListItem object's subitem(1).
   ' Use the Phone field for the ListItem object's subitem(2).

   While Not myRs.EOF
      
      Set itmX = ListView1.ListItems.Add(, , CStr(myRs!Name))

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

Private Sub ListView1_ColumnClick(ByVal ColumnHeader As ColumnHeader)
   ListView1.SortKey = ColumnHeader.Index - 1
   ListView1.Sorted = True
End Sub