You are currently offline, waiting for your internet to reconnect

ACC: Sample Function to Fill a ListView Control

This article was previously published under Q155178
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.
This article shows you how to fill a ListView control on a form withthe contents of a table or query.

This article assumes that you are familiar with Visual Basic forApplications and with creating Microsoft Access applications using theprogramming tools provided with Microsoft Access. For more informationabout Visual Basic for Applications, please refer to your version of the"Building Applications with Microsoft Access" manual.
The ListView control is one of the ActiveX controls (or Custom Controls inMicrosoft Access 7.0) included with the Microsoft Office 97 DeveloperEdition and the Microsoft Access Developer's Toolkit for Windows 95. Itdisplays a list of objects in one of four different views. You canarrange items into columns with or without column headings as well asdisplay accompanying icons with text. If you want to use the ListViewcontrol, you must fill it with data programmatically.

The following example uses the Employees table in the sample databaseNorthwind.mdb. The sample function FillList() populates a ListView controlwith data and displays it in one of its four different views, the Reportview.

  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.
For more information about the ListView control and its properties andmethods, search the Help Index for ListView control.

Article ID: 155178 - Last Review: 01/19/2007 17:44:13 - Revision: 3.3

  • Microsoft Access 95 Standard Edition
  • Microsoft Access 97 Standard Edition
  • kbhowto kbprogramming KB155178