PDA

View Full Version : Solved: Listview bug



lifeson
03-04-2008, 01:05 AM
I am experiencing a bug with a list view control that is driving me nuts :bug:

I use this bit of code to populate the listview and it works fine


Private Sub CheckQty()
Dim x As Long, i As Long
Dim chkRow As Long
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("BlrCopy")
chkRow = ws.Cells(Rows.Count, "A").End(xlUp).row - 1
Application.ScreenUpdating = False
If chkRow >= 1 Then
x = 1
With lvwBoilers
.ListItems.Clear
For i = 2 To chkRow + 1 'set range to search
.ListItems.Add , , ws.Cells(i, "A").Value
.ListItems(x).ListSubItems.Add , , ws.Cells(i, "B").Value
.ListItems(x).ListSubItems.Add , , ws.Cells(i, "C").Value
.ListItems(x).ListSubItems.Add , , ws.Cells(i, "D").Value
.ListItems(x).ListSubItems.Add , , ws.Cells(i, "E").Value
.ListItems(x).ListSubItems.Add , , ws.Cells(i, "F").Value
x = x + 1
Next I
End With
TextBox1.Value = chkRow & " items match the criteria"
Else
lvwBoilers.ListItems.Clear
TextBox1.Value = chkRow & " items match the criteria, try alternative inputs"
End If
End Sub[/vba]

I use this bit of code to allow the listview to be sorted when the columnheaders are clicked and it also works fine.

[Code]Private Sub lvwBoilers_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
With lvwBoilers
Static iLast As Integer, iCur As Integer
.Sorted = True
iCur = ColumnHeader.Index - 1
If iCur = iLast Then .SortOrder = IIf(.SortOrder = 1, 0, 1)
.SortKey = iCur
iLast = iCur
End With
End Sub

However, after the code has run to sort the columns, the code to poulate the list box doesn't work correctly.
It fills the first column of the list box but all other columns are empty except that last row.

Does anything look obviously wrong?

lifeson
03-04-2008, 07:27 AM
OK as they say a picture speaks a thousand words, therfore I have stripped it down to create a demo

After doing a bit more searching I think it may be something to do with the listview index property rather than the way the listview is poulated. but I'm only guessing

Bob Phillips
03-04-2008, 07:44 AM
What's the problem? Apart from the Make dropdown not properly re-loading, it seems to work fine AFAICS.

lifeson
03-04-2008, 07:47 AM
What's the problem? Apart from the Make dropdown not properly re-loading, it seems to work fine AFAICS.

When you click on one of the column headings of the listview control and then use a drop down filter, the lsitview doesnt populate correctly, only the first column is gettng filled.

Bob Phillips
03-04-2008, 09:32 AM
Try this



Private Sub CheckQty()
Dim x As Long, i As Long
Dim chkRow As Long
Dim wsCopy As Worksheet
Dim itmListView As ListItem
Set wsCopy = ThisWorkbook.Worksheets("BlrCopy")
chkRow = wsCopy.Cells(Rows.Count, "A").End(xlUp).row - 1
Application.ScreenUpdating = False
If chkRow >= 1 Then
With lvwBoilers
.ListItems.Clear
For i = chkRow + 1 To 2 Step -1 'set range to search
Set itmListView = .ListItems.Add(, , wsCopy.Cells(i, "A").Value) 'premID
itmListView .ListSubItems.Add , , wsCopy.Cells(i, "B").Value
itmListView .ListSubItems.Add , , wsCopy.Cells(i, "C").Value
itmListView .ListSubItems.Add , , wsCopy.Cells(i, "D").Value
itmListView .ListSubItems.Add , , wsCopy.Cells(i, "E").Value
itmListView .ListSubItems.Add , , wsCopy.Cells(i, "F").Value
Next I
End With
TextBox1.Value = chkRow & " Boilers match the criteria"
Else
lvwBoilers.ListItems.Clear
Msg = "No boilers match the criteria!"
ans = MsgBox(Msg, vbInformation)
TextBox1.Value = chkRow & " Boilers match the criteria, try alternative inputs"
Dim fPath As String
fPath = ThisWorkbook.path & "\Media\"
On Error Resume Next 'prevents error if media folder is not present
Image1.Picture = LoadPicture(fPath & "None.jpg")
lblimage.Caption = "No boiler selected"
End If
Sheets("front").Select
End Sub

lifeson
03-04-2008, 09:50 AM
Thanks for looking XLD

I am getting an error with that code though

"Method or Data member not found"

with

itmListView .ListSubItems.Add, , wsCopy.Cells(i, "B").Value

Bob Phillips
03-04-2008, 10:16 AM
Sorry, we seem to have introduced spaces. Remove the space between itmListView and .ListSubItems for all of those lines of code.

lifeson
03-04-2008, 10:26 AM
No need to apologise
I should have spotted that myself

As usual :clap: :clap: :bow: