Better filter/criteria (1 Viewer)

anski

Registered User.
Local time
Tomorrow, 05:51
Joined
Sep 5, 2009
Messages
93
I have a report that should show all colors except the colors indicated in 5 fields. My filter (found in Report Property Sheet) for the report is this:

color <> DLookUp("color1","tbl_color") and
color <> DLookUp("color2","tbl_color") and
color <> DLookUp("color3","tbl_color") and
color <> DLookUp("color4","tbl_color") and
color <> DLookUp("color5","tbl_color")

The above works but is there a better way to construct this filter?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 14:51
Joined
Oct 29, 2018
Messages
21,474
Maybe try the InStr() function?
 

CJ_London

Super Moderator
Staff member
Local time
Today, 22:51
Joined
Feb 19, 2013
Messages
16,616
If your filter is permanent, include in the report recordsource as criteria instead
 

Josef P.

Well-known member
Local time
Today, 23:51
Joined
Feb 2, 2023
Messages
827
If tbl_color was normalized, this could work:
SQL:
color not in (select color from tbl_color where ColorNo between 1 and 5)
 

plog

Banishment Pending
Local time
Today, 16:51
Joined
May 11, 2011
Messages
11,646
I liked Josef's solution but need to comment to:

What happens when you need to add a 6th color? Or subtract a color? Rhetorical question--you not only need to update your table structure but all your code that references that old table structure. A super pain for a simple change.

If instead tbl_color is a list of colors to exclude you should just have 1 field in it and then 5 records. That way if you need to add or subtract any it doesn't matter how many are in there--it finds and uses however many are there--you just add/delete records in tbl_color.

So to modify Josef's code, this is what you ultimately should aim for:

Code:
color not in (select color from tbl_color)
 

anski

Registered User.
Local time
Tomorrow, 05:51
Joined
Sep 5, 2009
Messages
93
I liked Josef's solution but need to comment to:

What happens when you need to add a 6th color? Or subtract a color? Rhetorical question--you not only need to update your table structure but all your code that references that old table structure. A super pain for a simple change.

If instead tbl_color is a list of colors to exclude you should just have 1 field in it and then 5 records. That way if you need to add or subtract any it doesn't matter how many are in there--it finds and uses however many are there--you just add/delete records in tbl_color.

So to modify Josef's code, this is what you ultimately should aim for:

Code:
color not in (select color from tbl_color)
I know what you are saying about one field and five records. i need separate fields because these 5 fields are used as columns for a report (5 columns). then in a subreport, these 5 "colors" should not be included anymore. my dilemma is, as you have mentioned, if in the future another "color"/column is added to the main report, i would have to update several objects.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 22:51
Joined
Feb 19, 2013
Messages
16,616
i need separate fields because these 5 fields are used as columns for a report (5 columns).
use a crosstab query to convert from vertical to horizontal.

You are making the classic error of trying to apply excel principles to a database, resulting in much more complex coding to overcome the shortfalls.

Do not confuse data storage with data views
 

Users who are viewing this thread

Top Bottom