Access query sort not working

I have a form that has a list box – there are two queries that serve as the data source for the list box
The users can switch between the data sources by clicking on an option on the form
Above the list box there are label fields that act as column headers and sort the data displayed in the list box
The first query works with no issues, the user can sort the data in the list and apply various filters
The second query has no additional filter options but I do want the users to be able to sort the list by the same options as the 1st query
The sort options work on all the columns of the second query except the ‘product description’ field.
The ‘product description’ field is the only long text field in the queries
The only difference between the queries is the 2nd used to exclude records from the 2nd query.
When you click on the ‘product description’ sort button it does change the order of the data but does not sort it correctly, if it is somehow sorted by some other field it’s not obvious, it does arrange the data differently ascending vs descending and they are always in the same order.
I’ve tested the 2nd query by adding the sort option on the product description in the query itself – it doesn’t work there either.  It seems to just be the combination of the long text field & the no records in the 2nd table.
Can anyone tell me why the sort on the 2nd query / ‘product description’ doesn’t work and how to fix it?

Here’s the code:

1st table -- tblProducts      
RecID      Long Integer
DateReceived      Short Date
ProductName      Text 255 char
ProductDescription      Long Text
Manufacturer      Text 100 char
ProductType      Text 50 char
ProductNumber      Text 15 char

2nd table -- tblSubParts

Query 1 – this is just a single table query
SELECT tblProducts.RecID, tblProducts.DateReceived, tblProducts.ProductName, tblProducts.ProductDescription, tblProducts.Manufacturer, tblProducts.ProductType, tblProducts.PartNumber FROM tblProducts;

Query2 – there are two tables here – showing the same table/fields as Query 1 only where there is no record in the second table
SELECT tblProducts.RecID, tblProducts.Received, tblProducts.ProductName, tblProducts.ProductDescription, tblProducts.Manufacturer, tblProducts.ProductType, tblProducts.PartNumber, tblSubParts.PartID
FROM tblProducts LEFT JOIN tblSubParts ON tblProducts.RecID = tblSubParts.RecID
WHERE (tblSubParts.PartID Is Null);

There is a global variable on the form strCurrentOrder that is used to remember the current sort order
This is one of the sort buttons

Private Sub lblSortby_ProdName_Click()
    If strCurrentOrder = "ProductName" Then
        strCurrentOrder = "ProductName DESC"
        strCurrentOrder = "ProductName"
    End If
    Call fRefreshSort
End Sub

The fRefreshSort function resets the Row Source depending on which query is being used

Me.lstMW.RowSource = "SELECT Query1.* FROM Query1 " & fFilter() & " ORDER BY " & strCurrentOrder

Me.lstMW.RowSource = "SELECT Query2.* FROM Query2 ORDER BY " & strCurrentOrder
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongSoftware Team LeadCommented:
the best way to debug it would be first put a break point at line:

Me.lstMW.RowSource = "SELECT Query1.* FROM Query1 " & fFilter() & " ORDER BY " & strCurrentOrder

and see what is being returned.

you may also add:


after the line: Me.lstMW.RowSource
Gustav BrockCIOCommented:
The ‘product description’ field is the only long text field in the queries

And that is why. The query will only sort on the first 255 characters - the remaining are simply ignored.
Your only option is to chop the long text into as many parts of 255 characters as needed and then sort on these. For example:

    Mid([product description],1+255*0,255), 
    Mid([product description],1+255*1,255), 
    Mid([product description],1+255*2,255),
    Mid([product description],1+255*3,255)

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fabrice LambertConsultingCommented:
I'm nowhere near conviced that sorting on a product description make any sens at all,.
Plus long text (or memo) type columns are somewhat capricious beasts, better not ask them too much beside the basic (retrieving data).

Side notes:
I don't think you need global variable for such trivial tasks, a form level private variable is enough.
Give up hungarian notation, it provides nothing usefull.
CountryGirlMDAuthor Commented:
Thanks All
Ryan - I had already done that - it didn't give any clue to why query1 worked and query2 didn't -- query 2 never gave me an actual error - it just didn't sort the data correctly

Gustav - I went with a simple version of your solution -- just a single left(ProductDescription,255) because very unlikely in my data for the first 255 to not be unique -- of 66,000 records -- only about 30 are over 255 -- max len 628

but it still bugs me that query1 will sort by the long text field and query2 won't

Fabrice - not my choice to sort by long text -- it's a user requirement -- and my bad -- I referred to the strCurrentOrder variable as "global" when it was actually a form level variable like you suggested
Gustav BrockCIOCommented:
but it still bugs me that query1 will sort by the long text field

It won't. If it seems to do, it is pure luck.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.