Microsoft KB Archive/166912

= ACC97: Listview Control Loses Data When Changing Properties =

Article ID: 166912

Article Last Modified on 1/19/2007

-

APPLIES TO


 * Microsoft Access 97 Standard Edition

-



This article was previously published under Q166912



Moderate: Requires basic macro, coding, and interoperability skills.



SYMPTOMS
When you toggle the Visible or Enabled properties of a ListView control, the control loses its records by approximately one-half each time that those properties are cycled.

NOTE: This occurs with Microsoft ListView Control, version 5.0, which is available with Microsoft Office 97 Developer Edition Tools.



RESOLUTION
Each time you toggle the Enabled or Visible properties of the ListView control, clear the ListView control and rerun code to fill the control with data. You can use the Clear method of the ListItems collection in the control to remove all the items. The following example uses a command button to toggle the Visible property of a ListView control without losing rows of data:  Follow steps 1 through 7 in the "Steps to Reproduce Problem" section later in this article.  Add a command button to the form. Set its Name property to Command0, and set its OnClick property to the following event procedure: Private Sub Command0_Click Dim db As Database, rs As Recordset Dim itmX As ListItem Set db = CurrentDb Set rs = db.OpenRecordset("Employees", dbOpenDynaset)

' Toggle the Visible property of the control. Me!ListView1.Visible = Not Me!ListView1.Visible

' Clear and then refill the control with data. While Not rs.EOF Set itmX = ListView1.ListItems.Add(,, CStr(rs![FirstName] )) If Not IsNull(rs![LastName]) Then itmX.SubItems(1) = CStr(rs![LastName]) End If           If Not IsNull(rs![BirthDate]) Then itmX.SubItems(2) = rs![BirthDate] End If           rs.MoveNext Wend End Sub  Save the form as frmListView, and then switch it to Form view. Note that as you click the command button to toggle the Visible property of the ListView control, the number of records displayed in the control remains the same.



STATUS
Microsoft has confirmed this to be a problem in Microsoft Office 97 Developer Edition Tools.



MORE INFORMATION
The Microsoft ListView Control, version 5.0, is one of the controls included in the ActiveX control file, Comctl32.ocx. This behavior exists in version 5.00.3601 of Comctl32.ocx.

Steps to Reproduce Problem
 Start Microsoft Access and open the sample database Northwind.mdb.</li> Create a new form not based on any table or query in Design view.</li> On the Insert menu, click ActiveX Control.</li> In the Insert ActiveX Control dialog box, select Microsoft ListView Control, version 5.0, and then click OK.</li> Set the following properties of the ListView control:

Name: ListView1

Width: 4"

Height: 2"

</li> On the View menu, click Code.</li>  Type the following procedure, which populates the ListView control with data when you open the form: Private Sub Form_Open(Cancel As Integer) Dim db As Database, rs As Recordset Dim itmX As ListItem Set db = CurrentDb Set rs = db.OpenRecordset("Employees", dbOpenDynaset)

ListView1.ColumnHeaders.Add,, "First Name", ListView1.Width/3 ListView1.ColumnHeaders.Add,, "Last Name", _ ListView1.Width/3, lvwColumnCenter ListView1.ColumnHeaders.Add,, "Birthdate", _ ListView1.Width/3 ListView1.View = lvwReport

While Not rs.EOF Set itmX = ListView1.ListItems.Add(,, CStr(rs![FirstName] )) If Not IsNull(rs![LastName]) Then itmX.SubItems(1) = CStr(rs![LastName]) End If           If Not IsNull(rs![BirthDate]) Then itmX.SubItems(2) = rs![BirthDate] End If           rs.MoveNext Wend End Sub </li>  Add a command button named Command0 to the form, and set its OnClick property to the following event procedure: Private Sub Command0_Click Me!ListView1.Enabled = Not Me!ListView1.Enabled End Sub </li> Save the form as frmListView, and then switch it to Form view. Note that as you click the command button to toggle the Enabled property of the ListView control, the number of records displayed in the control reduces by about half. However, after the number of records is reduced to one, the last record continues to display even if you continue to click the command button.</li></ol>

<div class="references_section">