Simplifying Query

bs0d

Well-known Member
Joined
Dec 29, 2006
Messages
622
I've built a query in Access that has a calculated field, combining strings from fields in multiple tables. I'd like to cut down on the length of the query by referencing this field also in the where clause. Is this possible?

For example, current query:
Code:
Select
table1.field & " " & table2.field & " " & table 3.field AS MY_FIELD

from table inner join (all the tables by a common key)

Where table1.field & " " & table2.field & " " & table 3.field <> "condition 1", table1.field & " " & table2.field & " " & table 3.field <> "condition 2";
Above, the where clause specifies the full calculated field. I'd like to just refer to it as "MY_FIELD", but it's not recognizing it. Is this possible?

Code:
Where MY_FIELD <> "condition 1" and MY_FIELD <> "condition 2";
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Access does not like using calculated field names in the criteria in the same query that they are defined/calculated. I am not sure the exact order of events that Access does. Perhaps it does the criteria selection before the calculated fields.

However, you can do want you want you either do this in series of queries, or if you use a sub query.

So, you just have your one query that does all your calculations and creates your calculated fields.
Then you have a second query that uses the first query as the source, and you can reference the calculated field names.
 
Upvote 0
Ok, I think that's a reasonable solution. Establish sort of a "base" query, then a query to filter by criteria and simplified filed names off that.

Thanks Joe.
 
Upvote 0
Yep, you got it.

I use this technique a lot when I have Criteria Selection Forms, where I allow the user to to select from a myriad of criteria, and when actually write the query in VBA behind the scene. Because the queries can be quite lengthy, I often create this "base query" with all the calculated and fields I need, and then in my VBA I simply use:
Code:
SELECT *
FROM MyBaseQuery
WHERE ...
So then in VBA, I am only really needing to create the Criteria on the fly, and not the whole entire query.

IMHO, it makes things easier to see, understand, and maintain (at least in that context I described above). There is nothing wrong with basing queries on other queries (of course, you probably want to avoid going overboard and nesting it too many levels deep).
 
Last edited:
Upvote 0

Forum statistics

Threads
1,221,819
Messages
6,162,155
Members
451,749
Latest member
zack_ken

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top