Share via

Access Database Dynamic Sort Query

Amedean 21 Reputation points
2021-12-05T20:07:43.007+00:00

What is MSDN doing with the Access Database developer forum? Where did it go? Anyways, I have been trying to figure out where I failed in my SQL statement for my query. Trying to use an IFF() to change the sort order based on the value of a TempVar. My "Sort By" code goes as follows. No errors or anything popping up in the code, it just has no effect on the sorting.

ORDER BY IIf([TempVars]![tpvFavoriteList1]="TitleAsc","[qryArchive].[ObjectName]","[tblNavFavorite].[GUID]");

So as it is, I store the type of sort I want to perform in a tempvar and basically I was going to use the IFF() to read the tempvar variable. It isn't doing anything trouble is....

Microsoft 365 and Office | Access | Development

1 answer

Sort by: Most helpful
  1. Erland Sommarskog 134.7K Reputation points MVP Volunteer Moderator
    2021-12-05T20:32:32.78+00:00

    As Viorel says, you should delete the double quotes for the sort columns:

    ORDER BY IIf([TempVars]![tpvFavoriteList1]="TitleAsc", [qryArchive].[ObjectName], [tblNavFavorite].[GUID]);
    

    With the quotes you are telling Access to sort on constant strings, which is not very useful.

    Next time you ask about Access try office-access for the tags. I'm not into Access myself, so I don't know what the tags are exactly, but I would expect it to be something like that.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.