Solved

Sorting listboxes at runtime

Posted on 2011-03-01
8
274 Views
Last Modified: 2012-05-11
I recently changed oversubforms to listboxes in a database because I needed to have a multi-select ability. Unfortunately the listbox control does not include a method for sorting on different columns at runtime as the subforms did.

I'm thinking of having a button for each column that would create the "ORDER BY" clause of the SQL statement that refreshed the rowsource property.

I figured before I tried to re-invent the wheel I would see if there were any methods to achieve this result.
0
Comment
Question by:wipnav
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 9

Expert Comment

by:McOz
Comment Utility
Just add the ORDER BY to the rowsource property of each listbox field

This has worked for me - good luck!
0
 
LVL 84
Comment Utility
There's no builtin functionality for that. You'll have to buildup your ORDER BY clause and reset the RowSource of your Listbox.
0
 
LVL 1

Author Comment

by:wipnav
Comment Utility
LSM, that is what I was thinking, but I would need to add a command button for each sortable column though?
0
 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
<I recently changed oversubforms to listboxes in a database because I needed to have a multi-select ability. >

you could have added a checkbox field for use in multi selection of items, and still have the sorting capabilities.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 500 total points
Comment Utility
I've done this with Labels, and running code on the Click or DoubleClick event of the labels. Command buttons would work as well.
0
 
LVL 75

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
You can also use an Option Group to make the selection, using the AfterUpdate event of the option group to determine with 'sorting' option was selected.

Private Sub frame1_AfterUpdate()
    Select Case 1
         Me.YourListBoxName.RowSource = "SELECT Table1.* FROM Table1 ORDER BY Table1.FIELD1"
    Select Case 2
         Me.YourListBoxName.RowSource = "SELECT Table1.* FROM Table1 ORDER BY Table1.FIELD2"
    Select Case 3
         Me.YourListBoxName.RowSource = "SELECT Table1.* FROM Table1 ORDER BY Table1.FIELD3"
 ' and so on
   End Select

End Sub

mx
0
 
LVL 75

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
minor correction: (not enough sleep last night)

Private Sub Frame1_AfterUpdate()
    Select Case Me.Frame1
       Case 1
         Me.YourListBoxName.RowSource = "SELECT Table1.* FROM Table1 ORDER BY Table1.FIELD1"
       Case 2
         Me.YourListBoxName.RowSource = "SELECT Table1.* FROM Table1 ORDER BY Table1.FIELD2"
       Case 3
         Me.YourListBoxName.RowSource = "SELECT Table1.* FROM Table1 ORDER BY Table1.FIELD3"
 ' and so on for the number of columns ...
   End Select

End Sub

mx
0
 
LVL 1

Author Closing Comment

by:wipnav
Comment Utility
Thanks LSM, out of all the solutions, this one seems to work the best. A sample that I used was from this tutorial: http://www.comboprojects.com/articles/accSortingList/accSortingListbox.html
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now