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.
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
- Open the sample database Northwind.mdb.
- Create a new form not based on any table or query. Save it as
frmListView and open it in Design view.
- 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
- On the View menu, click Properties. Set the following properties of the
- Create a module and type the following line in the Declarations
section if it is not already there:
- Type the following procedure:
NOTE: If you try to compile this function before you insert the ListView
control into your form, you receive the following error:
Function FillList(Domain As String, LV As Object) As Boolean
' Purpose: to fill a ListView control with data from a table or
' 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
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.
' 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)
LV.View = 3 ' Set View property to 'Report'.
' Set Total Records Counter.
intTotCount = Rs.RecordCount
' 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))
Set NewLine = LV.ListItems.Add(, , Rs(0).Value)
For IntCount2 = 1 To Rs.Fields.Count - 1
NewLine.SubItems(intCount2) = Rs(intCount2).Value
' Ignore Error 94 which indicates you passed a NULL value.
If Err = 94 Then
' Otherwise display the error message.
MsgBox "Error: " & Err.Number & Chr(13) & _
Chr(10) & Err.Description
User-defined type not defined
- Set the OnLoad property of the frmListView form to the following event
Private Sub Form_Load()
Dim intResult as Integer
intResult = FillList("Employees",Me!ctlListView)
- 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 and
methods, search the Help Index for ListView control
Article ID: 155178 - Last Review: January 19, 2007 - Revision: 3.3
- Microsoft Access 95 Standard Edition
- Microsoft Access 97 Standard Edition
|kbhowto kbprogramming KB155178|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.