ACC: Sample Function to Fill a ListView Control

Last reviewed: August 29, 1997
Article ID: Q155178
The information in this article applies to:
  • Microsoft Access versions 7.0, 97

SUMMARY

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

This article describes a method 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."


Additional query words: adt
Keywords : IntpCstm kbprg PgmHowTo FmsHowTo
Version : 7.0 97
Platform : WINDOWS
Hardware : x86
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: August 29, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.