Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to Sort my ListView 2

Status
Not open for further replies.

beckwiga

Programmer
Mar 30, 2005
70
Want to sort my ListView when user clicks a column heading. I have the following code which I believe is correct. Getting the following error, anyone know what this means? This is all VBA by the way.


Here is my code for the listview sort:


Private Sub ListView0_ColumnClick()

With ListView0
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



Here's my code for the Listview on form open code. This part works fine. This is pretty much all the code I have so far for the entire form.



Private Sub Form_Open(Cancel As Integer)

Dim rs As DAO.Recordset
Dim db As Database
Dim iCount As Integer
Dim LIX As ListItem


ListView0.ListItems.Clear
ListView0.ColumnHeaders.Clear

Set db = CurrentDb
Set rs = db.OpenRecordset("Select * From ListView0")


For iCount = 0 To rs.Fields.Count - 1
ListView0.ColumnHeaders.Add , , rs.Fields(iCount).Name
Next

Do While Not rs.EOF
For iCount = 0 To rs.Fields.Count - 1
If iCount = 0 Then
Set LIX = ListView0.ListItems.Add
LIX.Text = rs.Fields(iCount).Value & ""

Else
LIX.SubItems(iCount) = rs.Fields(iCount).Value & ""
End If
Next
rs.MoveNext
Loop
rs.Close
Set rs = Nothing

End Sub
 
No error on this line ?
iCur = ColumnHeader.Index - 1

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi PHV. It's not complaining about that line specifically. Do you see problem there? This is all stil new to me. I have problems understanding these errors.
 
I'd replace this:
iCur = ColumnHeader.Index - 1
With this:
iCur = .ColumnHeader.Index - 1

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Ahh, yeah. Thanks for the extra set of eyes, I missed that. Still getting the same error though. Do you think I need to add an Event Handler and a ListViewItemComparer
to my code similar to this?


Will this even work in VBA? I started messing around with the examples on this page, but ran into several errors, I think which were syntax related because I was trying in VBA. Is there an easier way that i don't know about?
 
Here is a sample code that sort ListView.This is working well in Access VBA. Date won't be sorted properly unless it is formatted "yyyy-mm-dd"
Code:
Private Sub ListView1_ColumnClick(ByVal ColumnHeader As Object)
'   When a ColumnHeader object is clicked,
'   the ListView control is
'   sorted by the subitems of that column.
'   Set the SortKey to the Index of the ColumnHeader - 1
    Me.ListView1.SortKey = ColumnHeader.Index - 1
    ' Set Sorted to True to sort the list.
    If ListView1.SortOrder = lvwAscending Then
        ListView1.SortOrder = lvwDescending
    Else
        ListView1.SortOrder = lvwAscending
    End If
    Me.ListView1.Sorted = True
End Sub
Looking into your code for loading ListView.
Code:
Set db = CurrentDb
Set rs = db.OpenRecordset("Select * From [b][COLOR=red]ListView0[/color][/b]")
there should be a table or query name; isn't it?

See this thread702-1044726 for more

________________________________________
Zameer Abdulla
Visit Me
There is only one perfect child in this world. Every mother has it.
 
Hi ZmrAbdulla. Thanks for the code. That's what I was looking for, and that seems to work great. The ListView0 from:

Set rs = db.OpenRecordset("Select * From ListView0")

..is actually my table name. My ListView is populating correcly. Thanks again for the sort.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top