ACC: Sample Function to Fill a ListView Control

Retired KB Content Disclaimer

This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.

Advanced: Requires expert coding, interoperability, and multiuser skills.


Summary

This article shows you how to fill a ListView control on a form with the contents of a table or query.


This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.

More Information

The ListView control is one of the ActiveX controls (or Custom Controls in Microsoft Access 7.0) included with the Microsoft Office 97 Developer Edition and the Microsoft Access Developer's Toolkit for Windows 95. It displays a list of objects in one of four different views. You can arrange items into columns with or without column headings as well as display accompanying icons with text. If you want to use the ListView control, you must fill it with data programmatically.


The following example uses the Employees table in the sample database Northwind.mdb. The sample function FillList() populates a ListView control with data and displays it in one of its four different views, the Report view.


  1. Open the sample database Northwind.mdb.
  2. Create a new form not based on any table or query. Save it as frmListView and open it in Design view.
  3. On the Insert menu, click ActiveX Control (or Custom Control in version 7.0). Click Microsoft ListView Control in the list of controls, and then click OK.
  4. On the View menu, click Properties. Set the following properties of the ListView control:
    Name: ctlListView
    Width: 5"
    Height: 2"
  5. Create a module and type the following line in the Declarations section if it is not already there:
    Option Explicit
  6. Type the following procedure:

           Function FillList(Domain As String, LV As Object) As Boolean
    '==================================================================
    ' Purpose: to fill a ListView control with data from a table or
    ' query
    ' Arguments: a Domain which is the name of the table or query, and
    ' a ListView control object
    ' Returns: A Boolean value to indicate if the function was
    ' successful
    '==================================================================

    Dim db As DATABASE, rs As Recordset
    Dim intTotCount As Integer
    Dim intCount1 As Integer, intCount2 As Integer
    Dim colNew As ColumnHeader, NewLine As ListItem

    On Error GoTo Err_Man

    ' Clear the ListView control.
    LV.ListItems.Clear
    LV.ColumnHeaders.Clear

    ' Set Variables.
    Set db = CurrentDb
    Set Rs = db.OpenRecordset(Domain)

    ' Set Column Headers.
    For intCount1 = 0 To Rs.Fields.Count - 1
    Set colNew = LV.ColumnHeaders.Add(, , Rs(intCount1).Name)
    Next intCount1
    LV.View = 3 ' Set View property to 'Report'.

    ' Set Total Records Counter.
    Rs.MoveLast
    intTotCount = Rs.RecordCount
    Rs.MoveFirst

    ' Loop through recordset and add Items to the control.
    For intCount1 = 1 To intTotCount
    If IsNumeric(Rs(0).Value) Then
    Set NewLine = LV.ListItems.Add(, , Str(Rs(0).Value))
    Else
    Set NewLine = LV.ListItems.Add(, , Rs(0).Value)
    End If
    For IntCount2 = 1 To Rs.Fields.Count - 1
    NewLine.SubItems(intCount2) = Rs(intCount2).Value
    Next intCount2
    Rs.MoveNext
    Next intCount1

    Exit Function

    Err_Man:
    ' Ignore Error 94 which indicates you passed a NULL value.
    If Err = 94 Then
    Resume Next
    Else
    ' Otherwise display the error message.
    MsgBox "Error: " & Err.Number & Chr(13) & _
    Chr(10) & Err.Description
    End If

    End Function
    NOTE: If you try to compile this function before you insert the ListView control into your form, you receive the following error:
    User-defined type not defined
  7. Set the OnLoad property of the frmListView form to the following event procedure:
          Private Sub Form_Load()
    Dim intResult as Integer
    intResult = FillList("Employees",Me!ctlListView)
    End Sub
  8. Open the form in Form view. Note that the ListView control displays the contents of the Employees table.

References

For more information about the ListView control and its properties and methods, search the Help Index for ListView control.
Properties

Article ID: 155178 - Last Review: Jan 19, 2007 - Revision: 1

Feedback