Solved

Listview sort order

Posted on 2010-11-30
6
272 Views
Last Modified: 2012-05-10
I have a listview with numbers in the first column which I would like to order sequentially.  At the moment my order is

1
10
100
1000
2
20
200
3
30
300

and so on,  I need it to be

1
2
3
10
20
30
100
200
300
1000

and so on.  Im guessing this is relatively simple but how do I achieve it?
0
Comment
Question by:simonwait
6 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 34239497
ListView - Sorting by Number or Date
http://www.codeguru.com/vb/controls/vb_listview/article.php/c1669


(by the way: is is .net or classic vb6 ?)
0
 
LVL 1

Author Comment

by:simonwait
ID: 34239594
So I think that should give me the following code however not im struggling with the "With" just before the sort (with objcurrli)

Oh its classic vb6
Private Sub LoadCustomerListView()
'-----------------------------------------------------------------------------
                                 
    Dim strSQL      As String
    Dim objCurrLI   As ListItem
    Dim strZip      As String
    Dim strPhone    As String
    Dim cueno As String
    Dim lngindex As Integer
                                 
    strSQL = "SELECT * FROM Act1 ORDER BY Q"
    ' "SELECT FirstName" _
           & "     , LastName" _
           & "     , Address" _
           & "     , City" _
           & "     , State" _
           & "     , Zip" _
           & "     , PhoneNumber" _
           & "     , CustID" _
           & "  FROM Act 1 " _
           & " ORDER BY Q" _

    
    mobjCmd.CommandText = strSQL
    Set mobjRst = mobjCmd.Execute
    
    lvwCustomer.ListItems.Clear
    
    With mobjRst
                cueno = ""
        Do Until .EOF
            Set objCurrLI = lvwCustomer.ListItems.Add(, , !Qno)
            If cueno = !Qno Then
            objCurrLI.SubItems(mlngQ_No_IDX) = "" & ""
            objCurrLI.SubItems(mlngQ_Number_IDX) = "" & ""
            objCurrLI.SubItems(mlngQ_Initiator_IDX) = "" & ""

            Else
            objCurrLI.SubItems(mlngQ_No_IDX) = !Q & ""
            objCurrLI.SubItems(mlngQ_Number_IDX) = !Initiator & ""
            objCurrLI.SubItems(mlngQ_Initiator_IDX) = !Description & ""
            End If
            objCurrLI.SubItems(mlngQ_Description_IDX) = !PB & ""
            objCurrLI.SubItems(mlngQ_PB_IDX) = !Axis & ""
            objCurrLI.SubItems(mlngQ_Axis_IDX) = !Dead & ""
            objCurrLI.SubItems(mlngQ_Dead_IDX) = !Pos & ""
            objCurrLI.SubItems(mlngQ_Position_IDX) = !Delay & ""
            objCurrLI.SubItems(mlngQ_Delay_IDX) = !Accel & ""
            objCurrLI.SubItems(mlngQ_Accel_IDX) = !Decel & ""
            objCurrLI.SubItems(mlngQ_Decel_IDX) = !Speed & ""
            objCurrLI.SubItems(mlngQ_Speed_IDX) = !CopyCalcTime & ""
            objCurrLI.SubItems(mlngQ_CopyCalc_IDX) = !StartPos & ""
            objCurrLI.SubItems(mlngQ_StartPos_IDX) = !TravelDist & ""
            objCurrLI.SubItems(mlngQ_TravelDistance_IDX) = !MoveTime & ""
            objCurrLI.SubItems(mlngQ_Time_IDX) = !Moveincdelay & ""
           ' objCurrLI.SubItems(mlngQ_TimeincDelay_IDX) = !Movetime
               
'            objCurrLI.SubItems(mlngCUST_ID_IDX) = CStr(!CustID)
cueno = !Qno
            .MoveNext
        Loop
    End With
    lngindex = 0
      
            With .objCurrLI
                If (lngindex > 0) Then
                    For l = 1 To .Count
                        With .Item(l).ListSubItems(lngindex)
                            .Tag = .Text & Chr$(0) & .Tag
                            If IsNumeric(.Text) Then
                                If CDbl(.Text) >= 0 Then
                                    .Text = Format(CDbl(.Text), _
                                        strFormat)
                                Else
                                    .Text = "&" & InvNumber( _
                                        Format(0 - CDbl(.Text), _
                                        strFormat))
                                End If
                            Else
                                .Text = ""
                            End If
                        End With
                    Next l
                Else
                    For l = 1 To .Count
                        With .Item(l)
                            .Tag = .Text & Chr$(0) & .Tag
                            If IsNumeric(.Text) Then
                                If CDbl(.Text) >= 0 Then
                                    .Text = Format(CDbl(.Text), _
                                        strFormat)
                                Else
                                    .Text = "&" & InvNumber( _
                                        Format(0 - CDbl(.Text), _
                                        strFormat))
                                End If
                            Else
                                .Text = ""
                            End If
                        End With
                    Next l
                End If
            End With

        
    

End Sub

Open in new window

0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 34240884
try

With objCurrLI
not
With .objCurrLI
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 17

Expert Comment

by:Zhaolai
ID: 34242243
An alternative is to add a hidden column with the same data but formatted like "00000000", then you can sort the hidden column as normal sorting.
0
 
LVL 53

Accepted Solution

by:
Dhaest earned 500 total points
ID: 34245735
Several ways to perform it:
1) use a MSFlexGrid, that allows the selection of the type of ordering, for each column (not only: it also allows to order using only a selected range of rows)
2) "pad" the items with blank characters to the left, so that the sort weel be correct even using an alphanumeric ordering. For eample, if you know that the numbers will have a maximum of 6 digits only, put the number into a string and them convert it using:
dim k as long
k = 6 - len(myString)
if k > 0 then myString = Space(k) & myString
3) listview sorting using API.
4) Say, your column name id Id, which is numeric, and assume that your id goes maximum upto 9999 then on Id column click, you can do the following: when you are loading the list view, give Format(!Id, "0000")
5) If you are getting the data from a database, it would be easier to sort it before you loaded it into the Listview.

Source: http://forums.devx.com/showthread.php?t=153973
0
 
LVL 1

Author Closing Comment

by:simonwait
ID: 34247483
I actually used the 2nd suggestion (padding) the column which w2as sorted on was already a hidden column so not too much of a leap to use the padding method
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question