tags:

views:

36

answers:

1

I have a listbox with values. Now I need to sort that listbox, BUT NOT BY ALPHABETICALLY.

You see the values in a listbox are from a table.

------------------------
| name | order |  size |
========================
 value1   4
 value2   3
 value3   1 
 value4   2

I hope I made myself clear. So the list box has the items "value1, value2, value3, value4". I want it to sort by the order declared in the table. The list box has nothing to do with the table and is not "data bound" to it. The values come from somewhere else.

Tech used: VBA, Access 2007

A: 

It sounds like the Row Source Type for your list box is "Value List". Since the values come from a table, change Row Source Type to "Table/Query", then use a query for Row Source:

SELECT [name], [order]
FROM YourTable
ORDER BY [order];

You don't have to display [order] in the list box ... set its column width to zero.

Your list box control does not have to be bound to a data source field for this approach to work. If the list box is unbound, the user selection will not be stored, but the list box will still allow users to make selections from the list box.

Notice I enclosed [name] and [order] in square brackets because both are reserved words. See Problem names and reserved words in Access

HansUp
Thankyou, Unfortunately I can not do that as the user is allowed to move/add/delete stuff from the listbox. Calling Additem/RemoveItem with a listbox with "Table/Query" produces an error.
masfenix
When the user adds an item to the list, does it get stored in the table which contains the other list values? If not, how do you assign [order] for the new item? Just place it at the end?
HansUp
User can only add specific items (from another listbox, but that is irrelevant). So he has buttons like " > ", " >> ", " < ", " << " to move items around. The items intially come from another table BUT these items are the COLUMN NAMES from the other table. Do you get what I mean?Table1's column names are the entries for Table 2.
masfenix
When the user adds an item to the list box, does that item value get stored in Table 2? Or any other table?
HansUp
No sir. However, I think i've found an alternative. Thankyou for your time, and enjoy the marked answer!
masfenix
Please post your solution as an answer, and accept it instead of mine. I don't care about the points. I'm more curious to understand what works. Your solution may also benefit someone else later.
HansUp
I don't really have a solution for this. I would post it if I did. I went for another alternative route instead (allowing the user to move the items in the listbox up/down so they can sort however they want)
masfenix