Select Distinct for combobox (1 Viewer)

fat controller

Slightly round the bend..
Local time
Today, 16:55
Joined
Apr 14, 2011
Messages
758
I am trying to get a combo box to only show unique values, but I can't get it to work properly - it still shows duplicates in the list.

The Row Source is set to

SELECT DISTINCT Areas.ID, Areas.Area FROM Areas ORDER BY Areas.Area;

And the combo is bound to column 2

Anyone able to give me a nudge in the right direction please?
 

jdraw

Super Moderator
Staff member
Local time
Today, 11:55
Joined
Jan 23, 2006
Messages
15,393
Please show us the records that are "duplicates" based on your query.
How are you differentiating unique and distinct?
 

MarkK

bit cruncher
Local time
Today, 08:55
Joined
Mar 17, 2004
Messages
8,187
Say you have data like . . .
Code:
ID, Area
1, Boston
2, Washington
3, Boston
4, Boston
. . . then your SQL will show three Bostons, because you have requested a DISTINCT Area.ID

You could do something like . . .
Code:
SELECT Min(ID) As ID, Area FROM Areas GROUP BY Area ORDER BY Area;
Which selects the Min(ID) for any duplicated Area name, but then certain IDs will never appear in your list.
 

fat controller

Slightly round the bend..
Local time
Today, 16:55
Joined
Apr 14, 2011
Messages
758
The attached is a screenshot of the table where the values for the combo come from.

In design view, I clicked on the three dots to the right hand side of the Row Source, which opened the query for the combo, and I then chose 'Yes' to Unique Values in the property sheet, saving the changes to the SQL statement on exit
 

Attachments

  • areas.png
    areas.png
    5.7 KB · Views: 94

jdraw

Super Moderator
Staff member
Local time
Today, 11:55
Joined
Jan 23, 2006
Messages
15,393
fat,
As Markk pointed out, duplicates would have same id and same Area.
 

fat controller

Slightly round the bend..
Local time
Today, 16:55
Joined
Apr 14, 2011
Messages
758
Say you have data like . . .
Code:
ID, Area
1, Boston
2, Washington
3, Boston
4, Boston
. . . then your SQL will show three Bostons, because you have requested a DISTINCT Area.ID

You could do something like . . .
Code:
SELECT Min(ID) As ID, Area FROM Areas GROUP BY Area ORDER BY Area;
Which selects the Min(ID) for any duplicated Area name, but then certain IDs will never appear in your list.

It really didn't like that, and gave an error (attached)
 

Attachments

  • err.png
    err.png
    30.2 KB · Views: 132

MarkK

bit cruncher
Local time
Today, 08:55
Joined
Mar 17, 2004
Messages
8,187
ooops, try . . .

Code:
SELECT Min([COLOR="Red"]Areas.[/COLOR]ID) As ID, Area FROM Areas GROUP BY Area ORDER BY Area;
 

fat controller

Slightly round the bend..
Local time
Today, 16:55
Joined
Apr 14, 2011
Messages
758
fat,
As Markk pointed out, duplicates would have same id and same Area.

Ah, so it needs more than just the area text itself to be the same. I am destined to see duplicates no matter what then?
 

fat controller

Slightly round the bend..
Local time
Today, 16:55
Joined
Apr 14, 2011
Messages
758
Re: ooops, try . . .

Code:
SELECT Min([COLOR=Red]Areas.[/COLOR]ID) As ID, Area FROM Areas GROUP BY Area ORDER BY Area;

You sir, are a gentleman and a scholar! Works perfectly, and as the ID's are never seen it matters not a jot that some might not show. :)
 

MarkK

bit cruncher
Local time
Today, 08:55
Joined
Mar 17, 2004
Messages
8,187
Love it when a plan comes together, :)
 

Jibbadiah

James
Local time
Tomorrow, 01:55
Joined
May 19, 2005
Messages
282
Just a note... If I knew that my area lookup had duplicate entries... A) I would dedupe them and fix all references. B) Remove the possibility of getting duplicates in future. It is worth the effort to stop future workarounds in other parts of your dB. James.
 

fat controller

Slightly round the bend..
Local time
Today, 16:55
Joined
Apr 14, 2011
Messages
758
Not entirely sure how I would do that? The table is primarily used for another form altogether where a route ID is selected by one combo box, and then a second shows the areas that are associated. This particular combo is a secondary, where I am effectively 'nicking' the data that is already there - no point having it twice?
 

Jibbadiah

James
Local time
Tomorrow, 01:55
Joined
May 19, 2005
Messages
282
Unless the region table has a sub-dimension (e.g. suburb) then it shouldn't have multiple entries for an Area. If there are multiple developers working on the database then you had better just stick with the status quo. If it is just under your management now I would take a back up and have a play to see if I could fix the duplicate entries. Somewhere there will be a reference table that refers to the Area table and stores the id. You could copy the region table (select and CTL C from the table menu... no need to open it). Paste a copy as "structure only" (CTL V). Open the copy table in design view. Put a primary key on the "Region" field. Copy your original table. Paste onto the new table using "append data to existing table". You will be left with a copy table that only has one entry for each region (the first record of each duplicate). Now you join the two tables on region and do an update query to update the id in your primary table with any reference that previously held it.

For your example the query should produce these results:

ID Region New_Id
1 Boston 1
2 Washington 2
3 Boston 1
4 Boston 1

It sounds hard, but it's actually quite easy and if you work with lots of data it is an invaluable trick to remove duplicates from source.
 

Attachments

  • DedupedRegionTable.png
    DedupedRegionTable.png
    21 KB · Views: 74
Last edited:

Users who are viewing this thread

Top Bottom