Microsoft KB Archive/210006

= MOD2000: Sample Function to Fill a ListView Control =

Article ID: 210006

Article Last Modified on 11/23/2006

-

APPLIES TO


 * Microsoft Office 2000 Developer Edition

-



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:

https://partner.microsoft.com/global/30000104

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:

http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS



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.

 Open the sample database Northwind.mdb.  Create the following form:   Form: frmListView ---  Caption: Employee List View RecordSource: None OnLoad: [Event Procedure]

 On the Insert menu, click ActiveX Control, select Microsoft ListView Control, version 6.0, and then click OK.  On the View menu, click Properties, and then set the following properties of the ListView control:   Name: ctlListView Width: 5"  Height: 2"   Create a module, and then type the following line in the Declarations section if it is not already there: Option Explicit </li>  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.

</li>  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 </li> Open the form in Form view. Note that the ListView control displays the contents of the Employees table.</li></ol>

Additional query words: filling adding populating

Keywords: kbinfo KB210006

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.