MOD2000: Sample Function to Fill a ListView Control (210006)



The information in this article applies to:

  • Microsoft Office 2000 Developer

This article was previously published under Q210006
Advanced: Requires expert coding, interoperability, and multiuser skills.

This article applies only to a Microsoft Access database (.mdb).

SUMMARY

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

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site: For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:

MORE INFORMATION

The ListView control is one of the ActiveX controls included with the Microsoft Office 2000 Developer. 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. To use the ListView control, you must populate the control 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 then displays it in one of its four different views, the Report view.

NOTE: The sample code in this article uses Microsoft Data Access Objects. For this code to run properly, you must reference the Microsoft DAO 3.6 Object Library. To do so, click References on the Tools menu in the Visual Basic Editor, and make sure that the Microsoft DAO 3.6 Object Library check box is selected.

  1. Open the sample database Northwind.mdb.
  2. Create the following form:
       Form: frmListView
       ---------------------------
       Caption: Employee List View
       RecordSource: None
       OnLoad: [Event Procedure]
    
    					
  3. On the Insert menu, click ActiveX Control, select Microsoft ListView Control, version 6.0, and then click OK.
  4. On the View menu, click Properties, and then set the following properties of the ListView control:
       Name: ctlListView
       Width: 5"
       Height: 2"
    					
  5. Create a module, and then type the following line in the Declarations section if it is not already there:
    Option Explicit
    					
  6. Type the following procedure:
    Function FillList(strDomain As String, objListView As Object) As Boolean
    '=======================================================================
    '   Purpose: To fill a ListView control with data from a table or query.
    '   Arguments:
    '            Domain = The name of the table or query.
    '                LV = The ListView control object.
    '   Returns: A Boolean value of True if the function was successful.
    '=======================================================================
        Dim dbs As DAO.Database
        Dim rst As DAO.Recordset
        Dim intTotCount As Integer
        Dim intCount1 As Integer
        Dim intCount2 As Integer
        Dim colNew As ColumnHeader
        Dim itmNewLine As ListItem
    
        On Error GoTo Err_Handler
    
        'Clear the ListView control.
        objListView.ListItems.Clear
        objListView.ColumnHeaders.Clear
    
        'Set Variables.
        Set dbs = CurrentDb
        Set rst = dbs.OpenRecordset(strDomain)
    
        'Set Column Headers.
        For intCount1 = 0 To rst.Fields.Count - 1
            Set colNew = objListView.ColumnHeaders.Add(, , rst(intCount1).Name)
        Next intCount1
    
        'Set View property to 'Report'.
        objListView.View = 3
    
        'Set Total Records Counter.
        rst.MoveLast
        intTotCount = rst.RecordCount
        rst.MoveFirst
    
        'Loop through recordset and add Items to the control.
        For intCount1 = 1 To intTotCount
            If IsNumeric(rst(0).Value) Then
                Set itmNewLine = objListView.ListItems.Add(, , _
                                 Str(rst(0).Value))
            Else
                Set itmNewLine = objListView.ListItems.Add(, , rst(0).Value)
            End If
    
            For intCount2 = 1 To rst.Fields.Count - 1
                itmNewLine.SubItems(intCount2) = rst(intCount2).Value
            Next intCount2
    
            rst.MoveNext
        Next intCount1
    
        Exit Function
    
    Err_Handler:
        If Err = 94 Then    'Ignore Error 94. You passed a Null.
            Resume Next
        Else
            MsgBox "Error: " & Err.Number & vbCrLf & Err.Description
        End If
    End Function
    					
    NOTE: If you try to compile this function before you insert the ListView control on your form, you receive the following Microsoft Visual Basic error:
    Compile 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.

Modification Type:MajorLast Reviewed:6/23/2005
Keywords:kbinfo KB210006