Hi everyone,
Is it possible to create a specific sort order in a query that's not
necessarily alphabetic or numeric? For example, I want to sort a list of
items in a specifc way:
Laptop
Television
Briefcase
Jeans
Khakis
See - they aren't alphabetic in order, but I need to see the most popular
items first. Can I do this in Access?
Thanks so much!
Sure.
Add a new table to the database.
(This is the best way. You could use an IIf expression in the query,
but then anytime you wish to change the order, or add a new Value to
sort, you must re-do the query. With a table, all you need do is
change the number value of the field, as below.)
Create a table with just 2 fields.
SomeFieldName Text datatype (contains the value to sort, i.e.
Laptop, Television, etc.
SortOrder Number datatype, Integer (contains the wanted sort order)
TableName "tblSortOrder"
Enter each choice in the in any order. Then ...
Enter the SortOrder number value in the order you wish it to sort
by in the report, i.e.
1 Laptop
4 Jeans
2 Television
5 Khakis
3 Briefcase
In the query add the tblSortOrder to the query.
Set the relationship between the 2 tables as SomeFieldName Inner Join
on SomeFieldName.
Then add a new column:
[SortOrder]
Sort the query on this field, Ascending.
The above assumes each SomeFieldName value will be one of the 5 in the
tblSortOrder.
If you have more than the 5 possible choices above, and you don't care
how the other choices are sorted, change the relationship between the
2 field's to a Left Join.
Then change the query column to:
SortThis: IIf([tblSortOrder].[SortOrder] Is Null,9999,[SortOrder])
The first 5 will still sort as you wish, all the remaining records
will sort according to no particular order after the first 5.
The nice thing about this method is that anytime you wish to change
the sorting order, all you need do is re-arrange the SortOrder
numbers. No further design changes are needed in the query.