Results 1 to 13 of 13

Thread: [RESOLVED] Load listview sorted

  1. #1

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Resolved [RESOLVED] Load listview sorted

    VB6 - Access 2002 - ADO

    I have this SQL statement, and I know that I need to use 'ORDER BY' to load into a certain order, but I can't seem to get it to work the way I am after.

    vb Code:
    1. strSQL = "SELECT First_Name, Middle_Name, Surname, Memb_ID FROM tbl_Membership"


    I am running this in the form load and then, the bit I am stuck on is trying to have the Listview sorted by Column 3 (Surname)

    How can I sort this by this column?

    Thanks
    Last edited by aikidokid; Jun 20th, 2007 at 02:40 PM.
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  2. #2

  3. #3

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Load listview sorted

    Thanks RhinoBull.

    I have this now. Actually I had this before, but the Listview Sorted property was set to true, so it was sorting it by the first column.

    That said, now I have set the Listview Sorted property set to False, and the listview is loaded how I would like, I can no longer let the user click on any of the column headers to change the sorting of the listview.

    How do I overcome this without changing the way the listview is sorted at form load time?
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  4. #4
    Fanatic Member amrita's Avatar
    Join Date
    Jan 2007
    Location
    Orissa,India
    Posts
    888

    Smile Re: Load listview sorted

    Use the below function and call for Column 3 on form load
    Code:
    Public Sub LV_ColumnSort(ListViewControl As ListView, _
      Column As ColumnHeader)
     With ListViewControl
      If .SortKey <> Column.Index - 1 Then
       .SortKey = Column.Index - 1
       .SortOrder = lvwAscending
      Else
       If .SortOrder = lvwAscending Then
        .SortOrder = lvwDescending
       Else
        .SortOrder = lvwAscending
       End If
      End If
      .Sorted = -1
     End With
    End Sub

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Load listview sorted

    Take your query out of the form load and put it into a Sub.

    Call the sub from the form load so that it has a base listing.

    Structure your sub in such a way as it can have multiple possibilities for sorting by adding a SortBy parameter which you will pass to it each time it is called.

    Now, on your listview, use the ColumnClick event so the users can decide how they want it sorted. In orders, if they click on the First Name column header you would reexecute your sub passing it the FirstName as a SortBy Parameter.

    Here is an example:
    Code:
    Option Explicit
    
    'create a varible to store the sortby string
    Private strSortBy As String
    
    Private Sub Form_Load()
    'establish a default
    strSortBy = "LName"
    LoadNames strSortBy
    End Sub
    
    Private Sub LoadNames(pstrSortBy As String)
    Dim strSQL As String
    
    strSQL = "SELECT First_Name, Middle_Name, Surname, Memb_ID FROM tbl_Membership "
         Select Case strSortBy
             Case "LName"
             strSQL = strSQL & "ORDER BY surname "
             Case "FName"
             strSQL = strSQL & "ORDER BY first_name "
             Case "MName"
             strSQL = strSQL & "ORDER BY middle_name "
         End Select
    Set rs = New ADODB.Recordset
    rs.Open strSQL, cn
    ListView1.ListItems.Clear
    'put code to load listview with recordset here
    rs.Close
    Set rs = Nothing
    End Sub
    
    Private Sub ListView1_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
    
    Select Case ColumnHeader
       Case "First Name"
          strOrderBy = "FName"      
       Case "Middle Name"
          strOrderBy = "MName"      
       Case "Last Name"
          strOrderBy = "LName"     
    End Select
    LoadNames strSortBy
    End Sub
    Now, in order to sort it, all they have to do is click on the column header that represents how they want it sorted.

  6. #6

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Load listview sorted

    Thanks again Hack

    I will look into this and have a go. I remember reading a thread a while back about using a select case for the SQL statement, but had forgotten about it.

    Just out of interest, was this something out of your code library, or did you just write it now?
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Load listview sorted

    I've been doing "ad hoc" SQL query building based on passed parameters for years.

    All of my reports are configurable by the end user. I have report criteria screen with checkboxs, option buttons, listboxs (that I used to be an IN clause) and sundry other controls. Once the user has selected what they want from the criteria screen and they click OK, I pass all of the selected options back to my report sub which in turn builds the SQL query. My querys are typically "infested" with If/Then and Select Case statements that add specific IN Clauses, AND statements, GROUP BY and ORDER BY clauses.

    The column click thing is something that came up as a requirement in my current project (although I've done similar things with listviews before).

  8. #8

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Load listview sorted

    A big thanks Hack for this help.

    I have got it working as you described, so thats good.
    But also, the concept of using the If/Select to build the SQL statement is something I will try to remember.
    I can see how it would enable me to make the program much more user friendly as well as giving it a hell of a lot more functionality.

    P.S. I noticed your deliberate mistake with the variables for sorting the columns
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  9. #9
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Load listview sorted

    Quote Originally Posted by aikidokid
    P.S. I noticed your deliberate mistake with the variables for sorting the columns
    I was wondering if you would notice that and correct it, or whether you would post back saying there was problem. Fine job.

    (PS: I do that sometimes just to see if who I'm dealing with is paying attention. )

  10. #10

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: [RESOLVED] Load listview sorted

    Quote Originally Posted by Hack
    I do that sometimes just to see if who I'm dealing with is paying attention.
    Most of the time I am paying attention, but sometimes I think I must be coding in my sleep!
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  11. #11

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Load listview sorted

    I have got all of this working (have had for a while now ) but what I would now like to do now, is when the user clicks on the columnheader, it sorts ascending, and if they then click on the same columnheader, sort it decending.
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  12. #12
    Fanatic Member amrita's Avatar
    Join Date
    Jan 2007
    Location
    Orissa,India
    Posts
    888

    Smile Re: Load listview sorted

    Try this function.
    Code:
    Private Sub ListView1_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
        
        'Check if the Sortkey is the same a the current one
        If ListView1.SortKey <> ColumnHeader.Index - 1 Then
    	ListView1.SortKey = ColumnHeader.Index - 1
            ListView1.SortOrder = lvwAscending
        Else
            'If the column is already selected then change the
            'sortorder to be the opposite of what is currently used
            
            ListView1.SortOrder = IIf(ListView1.SortOrder = lvwAscending, _
                                    lvwDescending, lvwAscending)
        End If
        
        ListView1.Sorted = True
    End Sub

  13. #13

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Load listview sorted

    Thanks amrita, this works exactly as I needed
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width