Search code examples
comboboxsql-order-byms-access-2016

MS Access combo box list order different from row source


My DB is tracks equipment being used at events.

  • [Asset] table tracks all assets (AssetId, AssetName, Make, Model...)
  • [Event] table tracks all events (EventId, EventName, Type, StartDate, ...)
  • [AssetEvent] table tracks which asset was used at which event

"frmUsage" is unbound, and has an Event drop down (working perfectly fine), and an "Asset" dropdown. I want the Asset dropdown to display the Assets ordered by AssetName, but to bound the AssetId value (to use in other parts of the form).

Asset's SQL is:

SELECT A.AssetId, A.AssetName FROM tbl_Asset AS A INNER JOIN tbl_AssetEvent 
AS AE ON A.AssetId = AE.AssetId WHERE (((AE.EventId) Like 
Nz(Forms!frmUsage.EventId,"*"))) GROUP BY A.AssetId, A.AssetName ORDER BY A.AssetName;

Behaviour:

  • Expected: When Event has been selected in the form, it should only show Assets associated to the form. Actual behaviour is as expected.
  • Expected: Dropdown values should show AssetName, and bind AssetId. Actual behaviour is as expected.
  • Expected: Dropdown values should be ordered by AssetName. Actual behaviour NOT as expected. Values are are ordered by AssetId.

How do I fix this?

Details: The combo box is configured with:

  • Column Count: 2
  • Column width: "0cm;2.5cm".
  • Row Source Type: Table/Query
  • Bound Column: 1
  • Limit to List: Yes

Solution

  • If you really need the AssetID grouping but the query is working other than that, then you can add it into a separate query inside and Order By outside that one.

    SELECT * FROM 
    (SELECT A.AssetId, A.AssetName FROM tbl_Asset AS A INNER JOIN tbl_AssetEvent 
    AS AE ON A.AssetId = AE.AssetId WHERE (((AE.EventId) Like 
    Nz(Forms!frmUsage.EventId,"*"))) GROUP BY A.AssetId, A.AssetName)
    ORDER BY A.AssetName;