Include a table in a Union Query based on Criteria from another table

jco23

Registered User.
Local time
Today, 17:55
Joined
Jun 2, 2015
Messages
71
I built a union query, combining the fields from table_A, table_B, and table_C.

However, I want to exclude table_C only if the inactive_date is not null (data stored in a master_list table). This master_list table lists all of the tables within the database.

I tried to insert some iif/then logic with the SQL code of the union query, but to no avail.

I see that I can enter an iif statement for each field, but I'd like to be able to enter it once for the whole table.

A.) Is this possible?
B.) what is the best method?

thanks!
 
Please tell readers in plain English What you are trying to achieve. Forget jargon for the moment. Readers will offer advice for HOW, once they understand What.
More info on table_A, table_B, and table_C also.
Instead of IF in SQL, you may be able to build some criteria in a WHERE clause..
 
Please tell readers in plain English What you are trying to achieve. Forget jargon for the moment. Readers will offer advice for HOW, once they understand What.
More info on table_A, table_B, and table_C also.
Instead of IF in SQL, you may be able to build some criteria in a WHERE clause..

I'm looking for the logic that will allow my union query to exclude a table based upon data from a separate table. can a union query be designed in this fashion?
 
You could use some vba. Have an IF or SELECT Case, then include or exclude one or more of the queries as appropriate.

(to exclude c)
Code:
If condition is true then
    sql = "select * from A " _
       & "UNION SELECT * from B " _
       & " Union SELECT * from C"
else
    sql = "select * from A " _
       & "UNION SELECT * from B "
end if
 

Users who are viewing this thread

Back
Top Bottom