Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sub Procedure for Filling Listview with Query Data

Status
Not open for further replies.

drewson

MIS
Jun 3, 2003
50
US
I've written a sub procedure to fill a listview with a query that I pass to it. I can't remember out how to dynamically reference the listview with the lvwItem so I can perform the procedure on different listviews. How can I do this?

Code:
Private Sub FillList(strSource As String, lvwItem As String)
    
    Dim rs As New ADODB.Recordset
    Dim colHeader As ColumnHeader
    Dim lstItem As ListItem
    Dim SQL As String
    Dim fld As Field
    Dim i As Integer
  
    Me[lvwItem].ListItems.Clear
    SQL = "SELECT * FROM " & strSource

    rs.Open SQL, CurrentProject.Connection, adOpenDynamic, adLockOptimistic
   
    lvwLocation.View = lvwReport
    
    For Each fld In rs.Fields
        lvwLocation.ColumnHeaders.Add , , fld.Name
    Next fld

    rs.MoveFirst
   
    While Not rs.EOF
         ' Add items and subitems to list control.
         Set lstItem = lvwLocation.ListItems.Add()
         
         lstItem.Text = rs.Fields(0)
         
         For i = 1 To rs.Fields.Count - 1
            lstItem.SubItems(i) = FormatCurrency(rs.Fields(i), 2)
         Next i

         rs.MoveNext
    Wend
    rs.Close
    For i = 2 To lvwLocation.ColumnHeaders.Count
        If IsNumeric(lvwLocation.SelectedItem.SubItems(i - 1)) Then
            lvwLocation.ColumnHeaders(i).Alignment = lvwColumnRight
        End If
    Next i
End Sub
 
Never mind... it came to me right after I submitted it. I just pass the listview control name as an object.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top