I've got a two-column listview that lists teams alphabetically and displays the number of cases associated with them. What if I wanted to sort the teams in descending order, or sort the list by the number of cases? Is this even possible with a listview?
I'm having a problem with that one...I implemented the code to the best of my knowledge but when I run it I'm getting this error:
Run-time error '35613':
ImageList must be initialized before it can be used
So I right-click my listview, go to properties, go to the imagelists tab and there are no image lists there. I don't have columns or anything in there either since they're added programmatically when the form loads. Is there a way to initialize the image list that way? I don't really know this object very well and what I've looked up on the internet has not been helpful.
And tweak it for numbers. Negative numbers become an issue because of the negative sign. For the long term and future projects look at Gavoi's post or Google for APIs that handle all that. Search this forum also. Some people assign numbers the user can't see and tweak the sort to use them.
And tweak it for numbers. Negative numbers become an issue because of the negative sign. For the long term and future projects look at Gavoi's post or Google for APIs that handle all that. Search this forum also. Some people assign numbers the user can't see and tweak the sort to use them.
I tried implementing the code that gavio referred to but I keep having problems with the imagelist; apparently I'm not initializing it but I don't know how to do it programatically. I can't do it in the properties of the object because I don't add columns to it until the form loads.
I tried implementing the code that gavio referred to but I keep having problems with the imagelist; apparently I'm not initializing it but I don't know how to do it programatically. I can't do it in the properties of the object because I don't add columns to it until the form loads.
This works for me. Its the formatting when you add items.
VB Code:
Option Explicit
Private iItem As ListItem
Private Sub Form_Load()
Set iItem = lvwEmailEvents.ListItems.Add(, , "Testing" & "")
iItem.SubItems(1) = Format$("1", "@@@@@@@@@@@")
Set iItem = lvwEmailEvents.ListItems.Add(, , "Testing" & "")
iItem.SubItems(1) = Format$("11", "@@@@@@@@@@@")
Set iItem = lvwEmailEvents.ListItems.Add(, , "Testing" & "")
iItem.SubItems(1) = Format$("155", "@@@@@@@@@@@")
Set iItem = lvwEmailEvents.ListItems.Add(, , "Testing" & "")
iItem.SubItems(1) = Format$("2", "@@@@@@@@@@@")
Set iItem = lvwEmailEvents.ListItems.Add(, , "Testing" & "")
iItem.SubItems(1) = Format$("6", "@@@@@@@@@@@")
End Sub
Public Sub SortListView(lvwList As MSComctlLib.ListView, ByVal ColumnHeader As MSComctlLib.ColumnHeader)
If lvwList.SortKey = (ColumnHeader.Index - 1) Then
If lvwList.SortOrder = lvwAscending Then
lvwList.SortOrder = lvwDescending
Else
lvwList.SortOrder = lvwAscending
End If
lvwList.Sorted = True
Else
lvwList.SortKey = (ColumnHeader.Index - 1)
lvwList.Sorted = True
lvwList.SortOrder = lvwAscending
End If
End Sub
Private Sub lvwEMailEvents_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
On Error GoTo ErrorHandler
SortListView lvwEmailEvents, ColumnHeader
Exit Sub
ErrorHandler:
Screen.MousePointer = vbNormal
MsgBox "Error lvwEMailEvents_ColumnClick at Line # " & Erl & " (" & Err.Description & ")", , "Error: lvwEMailEvents_ColumnClick"
I just plugged it into a project with the numbers you listed and it works fine. You are clicking on the column header to sort them right?
If you are expecting them to be sorted when you load them you need to do that when gathering data or set it on the properties of the listview.
I'm not trying to be cute...just asking.
They're already sorted correctly when they load; the listview is supposed to show the two columns of data in Teams order, not Cases (a number) order.
If I implement this code it 'works' but it doesn't, if that makes sense. It sorts the Team (alpha) list just fine. However, it sorts the numbers incorrectly, i.e. it sorts them as if they are text instead of numbers. I did say this once already.
I also tried to implement the code that gavio referred to. I noted that I was having problems with it that I still have not worked out the solution to, and nobody has offered to step in and help with that. So I guess my app won't have a sortable listview; it isn't the end of the world.
Don't know what you mean... Please give examples and possibly code.
My listview has two columns. One column has a list of teams. The other column has the number of cases associated with each team. I don't know any other way to explain it. Say like you have a sequence of numbers...1, 2, 4, 10, 15, 20, 35. When I sort by the number of cases, it is sequenced this way: 1, 10, 15, 2, 20, 35, 4. This is incorrect.
This is the code that populates the listview when the form loads. It's probably crap, but it was my first and only attempt at using a listview. It loads fine, it just doesn't sort correctly. The code I was using to sort was the same one posted by TysonLPrice.
VB Code:
Public Sub PopTeamList()
Dim strSQL As String
Dim rst As ADODB.Recordset
Dim lstTeam As ListView
Dim itmx As ListItem
Dim colx As ColumnHeader
Call CheckConnection
' Source of the Team list
strSQL = "SELECT Team, Cases FROM vwFETeamList WHERE Team IS NOT NULL ORDER BY Team"
Yes, now that I've added it it does work. Thank you. I don't mean to appear short, I've just been too busy recently to give this my full attention; it fell quite low on my priority list!
Yes, now that I've added it it does work. Thank you. I don't mean to appear short, I've just been too busy recently to give this my full attention; it fell quite low on my priority list!
Great! There are limitations to that fix with negative numbers. VB puts a sign in front of them and then you get the alpha sorting problem again. Some of those other links address that but for whole numbers what I suggested (I got it from searching here) works fine.
If you feel this is resolved edit your original post and add "Resolved" to the title so people will stop trying to work it.
No kidding... I'm the one that can read the post... remember
Randem, I did download the code you offered and tried it; I had the same problems with the icons that I had earlier in the thread. Tyson's solution worked and was very simple, i.e. it required the least amount of effort to make the change, so I used it. I do appreciate everybody's help, though. I'm sorry you felt ignored.