Results 1 to 13 of 13
  1. #1
    Brightspark98 is offline Advanced Beginner
    Windows 7 64bit Access 2013 32bit
    Join Date
    Dec 2016
    Posts
    57

    Listbox Sort

    Hi,



    Is it possible to sort a listbox ascending or descending by either clicking on the header or by use of buttons?

    The listbox in question is 4 columns wide with the ID column hidden.

    Data comes from two tables.

    Many Thanks

  2. #2
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Only manually - by requerying the listbox with a different SQL string (same fields, different sort)

  3. #3
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    In case you read that wrong, what I meant by "manually" is thru VBA or a macro when a user clicks on a button.

  4. #4
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    I haven't done this with a list box, but have with a continuous form. Is your list box based on a query (hopefully) or is it filtered? If a query, you'd alter the sql behind the list box according to the sort as aytee111 says.

    Some things you could do to play around:
    - hide the column headers and use your own command or toggle buttons above one or more list box columns
    - setting a command button back style to transparent, you could place a colored rectangle behind the button and color it if sorted by that column.
    - or just use rectangles, coloring as noted, or switching between raised and sunken
    or whatever else turns your crank!
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    Brightspark98 is offline Advanced Beginner
    Windows 7 64bit Access 2013 32bit
    Join Date
    Dec 2016
    Posts
    57
    Hi,

    This is the SQL for the listbox,

    Can anyone help on how to assign it to three buttons in order to sort the data in each column.

    SELECT tbl_T_Trainee.Surname, tbl_T_Trainee.Forename, GroupRef.GroupRef
    FROM GroupRef INNER JOIN tbl_T_Trainee ON GroupRef.GroupRefID = tbl_T_Trainee.GroupRefID
    ORDER BY tbl_T_Trainee.Surname, tbl_T_Trainee.Forename, GroupRef.GroupRef;

    Once again

    Many thanks

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Behind each button:

    Me.ListBoxName.RowSource = "SELECT..."

    where the SQL is as you show it, but with the ORDER BY clause adjusted as appropriate to the button. I'd use a variable for the SQL, but it isn't required.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    a bit more info is needed. If there are 3 buttons, 1 for each column and I click the leftmost button, is it supposed to sort ascending or descending? Whichever that will be, what if I click it again? Maintain the same sort or switch it to the opposite?
    EDIT:
    What about the other 2 columns when I sort on just one (default is ascending).
    Last edited by Micron; 01-25-2017 at 05:06 PM. Reason: additional info

  8. #8
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    Well, out of curiosity, I went ahead with a routine that would reverse a sort each time the button was clicked. However, it became apparent that to worry about the sort of the remaining columns beyond settling for the default ascending sort would be much more involved. Rather than repeat the sql in each button event, plus the fact that knowing the last sort is required, I opted for a function that managed both.

    For this to work as I have coded it, create 3 command buttons but you MUST name each according to the field you want to sort by, such as cmdSurname. On the property sheet for each button (Other tab), find the Tag property and put 'asc' there (without any quotes). To make it easier for me, I will give one example and use cmdBtn1 as my name - substitute the real name for that AND the listbox. Also, at the top of the form module where you (should) see
    Option Compare Database
    Option Explicit
    put this below those lines:
    Code:
    Const strSql = "SELECT tbl_T_Trainee.Surname, tbl_T_Trainee.Forename, GroupRef.GroupRef " & _
    "FROM GroupRef INNER JOIN tbl_T_Trainee ON GroupRef.GroupRefID = tbl_T_Trainee.GroupRefID ORDER BY "
    For the click event of each button:

    Code:
    Private Sub cmdBtn1_Click
    
    Dim strSort As String
    'pass button and button tag property to function and assign function value to strSort variable
    strSort = strSql & PassTag (Me.cmdBtn1, Me.cmdBtn1.Tag)
    Me.lstMyListbox.Rowsource = strSort
    Me.lstMyListbox.Requery
    
    End Sub
    This function goes in the form module
    Code:
    Private Function PassTag (ctl As Control, strTag As String) As String
    
    'function receives control as object so that it can affect its tag value
    If strTag = "asc" Then 
      ctl.Tag = "desc"
    Else
      ctl.Tag = "asc"
    End If
    
    'if control name contains the word "name", prepend appropriate table name and part of button name that matches
    ' name that matches the listbox column and add this to the ORDER BY clause
    If InStr(ctl.Name, "name") > 0 Then
      PassTag = "tbl_T_Trainee." & Mid(ctl.Name, 4) & " " & ctl.Tag & ";"
    Else
    'else it is the GroupRef table we need to include in the ORDER BY clause
      PassTag = "GroupRef.GroupRef " & ctl.Tag & ";"
    End If
    
    End Function
    The form should open as it does now, and the click of a column button should reverse the sort on that column if you make the necessary changes.
    NOTE - this is 'air code' and is not fully tested as I don't have your db to work with. Hope it helps.
    If you try it and it doesn't work, you need to post your version of the code along with any error message numbers and text if you want help on this code.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  9. #9
    Brightspark98 is offline Advanced Beginner
    Windows 7 64bit Access 2013 32bit
    Join Date
    Dec 2016
    Posts
    57
    Hi Micron,

    Thanks for all of your efforts so far, I think I must be doing something wrong as I cant get the code to work. When I click the button the data in the listbox disappears, if I refresh I get the message shown below. I couldn't find the module with Option Explicit so I entered this manually, was this correct?
    All of the code is in the same place please see below also.

    Code:
    Option Compare DatabaseOption Explicit
    
    
    Const strSql = "SELECT tbl_T_Trainee.Surname, tbl_T_Trainee.Forename, GroupRef.GroupRef " & _
    "FROM GroupRef INNER JOIN tbl_T_Trainee ON GroupRef.GroupRefID = tbl_T_Trainee.GroupRefID ORDER BY "
    
    
    
    
    Private Sub cmdForename_Click()
    Dim strSort As String
    'pass button and button tag property to function and assign function value to strSort variable
    strSort = strSql & PassTag(Me.cmdForename, Me.cmdForename.Tag)
    Me.lbTrainee.RowSource = strSort
    Me.lbTrainee.Requery
    End Sub
    
    
    Private Sub cmdGroupRef_Click()
    Dim strSort As String
    'pass button and button tag property to function and assign function value to strSort variable
    strSort = strSql & PassTag(Me.cmdGroupRef, Me.cmdGroupRef.Tag)
    Me.lbTrainee.RowSource = strSort
    Me.lbTrainee.Requery
    End Sub
    
    
    Private Sub cmdSurname_Click()
    Dim strSort As String
    'pass button and button tag property to function and assign function value to strSort variable
    strSort = strSql & PassTag(Me.cmdSurname, Me.cmdSurname.Tag)
    Me.lbTrainee.RowSource = strSort
    Me.lbTrainee.Requery
    End Sub
    Private Function PassTag(ctl As Control, strTag As String) As String
    
    
    'function receives control as object so that it can affect its tag value
    If strTag = "asc" Then
      ctl.Tag = "desc"
    Else
      ctl.Tag = "asc"
    End If
    
    
    'if control name contains the word "name", prepend appropriate table name and part of button name that matches
    ' name that matches the listbox column and add this to the ORDER BY clause
    If InStr(ctl.Name, "name") > 0 Then
      PassTag = "tbl_T_Trainee." & Mid(ctl.Name, 4) & " " & ctl.Tag & ";"
    Else
    'else it is the GroupRef table we need to include in the ORDER BY clause
      PassTag = "GroupRef.GroupRef " & ctl.Tag & ";"
    End If
    
    
    End Function
    Click image for larger version. 

Name:	Message.png 
Views:	22 
Size:	40.5 KB 
ID:	27251

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Try this to see how the finished SQL is coming out:

    BaldyWeb-Immediate window
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  11. #11
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    yes, do as pbaldy suggests and post the output if you see nothing wrong with it and/or a compacted, zipped copy of your db, but while I'm here, I'll ask
    - this was the original record source for the list box and there is no row source specified for it, yes?

    Code:
    SELECT tbl_T_Trainee.Surname, tbl_T_Trainee.Forename, GroupRef.GroupRef
    FROM GroupRef INNER JOIN tbl_T_Trainee ON GroupRef.GroupRefID = tbl_T_Trainee.GroupRefID
    ORDER BY tbl_T_Trainee.Surname, tbl_T_Trainee.Forename, GroupRef.GroupRef;
    Last edited by Micron; 01-26-2017 at 05:52 PM. Reason: clarification

  12. #12
    Brightspark98 is offline Advanced Beginner
    Windows 7 64bit Access 2013 32bit
    Join Date
    Dec 2016
    Posts
    57
    Thanks for all of your help.

    I assigned the sql to two cmd buttons above each colums, one for ascending, one for descending. All seems to work as I wanted.

    Thank you.

  13. #13
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    Glad you got it working, but too bad we didn't learn what the problem was. Two of something (buttons) are not always better when one should do.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 04-07-2014, 02:59 AM
  2. Replies: 2
    Last Post: 03-23-2014, 06:50 AM
  3. Replies: 8
    Last Post: 09-01-2013, 01:07 PM
  4. sort filter, doesn't sort everything
    By deso in forum Queries
    Replies: 4
    Last Post: 01-25-2013, 05:27 AM
  5. Replies: 1
    Last Post: 09-10-2012, 11:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums