Results 1 to 7 of 7
  1. #1
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904

    Change Sort Order of Combo Box "On-The-Fly"


    I have a Combo Box used on one of my Forms for record selection. It shows four fields, though only the first one is bound:
    1. Company Code
    2. Plan Code
    3. Plan Name 1
    4. Plan Name 2

    It is currently set to sort by the Company Code field, which is the bound field.

    Over the years, some companies have changed their plan names, and now our users would like the ability to look up by the Plan Name 1 field.
    So what I would like to do is somehow give them the ability to toggle how the Combo Box entries are sorted (either by the first or third fields).

    How can I do that?

    Thanks

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,543
    You could use an option group control or another combo box for the user to select the sort order. In it's after Update event put some code to determine the selection and then change the Row source property of the combo box.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Thanks Bob! That got me where I need to go! I added another combo box for them to choose which sort option, and then added the code to the AfterUpdate event, like you recommended.

    In case this helps anyone in the future, I am posting my code below.
    Code:
    Private Sub cboOrder_AfterUpdate()
    '   Change order of plan selection combo box
        
        Dim mySQL As String
        
    '   Build beginning of SQL code for plan selection combo box
        mySQL = "SELECT strClientCode, strEngageNum, strPlanName1, strPlanName2 FROM [tbl Plan Info]"
        
    '   Determine how to sort and update SQL code
        Select Case cboOrder.Value
            Case "Company Code"
                mySQL = mySQL & " ORDER BY 1, 2;"
            Case "Plan Name 1"
                mySQL = mySQL & " ORDER BY 3, 4;"
            Case Else
                mySQL = mySQL & " ORDER BY 1, 2;"
        End Select
            
    '   Apply SQL code to plan selection combo box
        Me.cboPlan.RowSource = mySQL
                
    End Sub

  4. #4
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,543
    Glad to help. Can you mark it as solved.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  5. #5
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Can you mark it as solved.
    I already marked it as solved over an hour ago!

  6. #6
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,543
    Quote Originally Posted by JoeM View Post
    I already marked it as solved over an hour ago!
    My apologies.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  7. #7
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    No worries! I appreciate the help!

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

Similar Threads

  1. Replies: 30
    Last Post: 09-27-2013, 01:34 PM
  2. Replies: 1
    Last Post: 12-28-2012, 02:54 PM
  3. Replies: 2
    Last Post: 10-01-2012, 06:47 PM
  4. Replies: 11
    Last Post: 03-15-2012, 01:36 PM
  5. Replies: 1
    Last Post: 10-19-2009, 02:37 AM

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