Union query or not

inademam

New member
Local time
Today, 08:05
Joined
May 10, 2011
Messages
3
I have what should be as easy query but for whatever reason it is making me crazy. I basically need to combine 3 different queries where the last column is one in the same with the different variables of the three queries. My 3 queries are:

62Billable: IIf([StaffFac]="62" And [SerCode]="90000",0,IIf([StaffFac]="62" And [SerCode]="90001",0,[StaffTime]))

6215Billable: IIf([StaffFac]="6215" And [SerCode]="90000",0,IIf([StaffFac]
="6215" And [SerCode]="90001",0,[StaffTime]))

90002Billable: IIf([SerCode]="90002",0,[StaffTime])

Each of these work by themselves.
So basically I want a zero for these otherwise give me the staff time. I
wrote another query to pull these together:

FinalBill: IIf([90002qry]!Billable="0",0,IIf([62Billable]="0",0,IIf(
[6215Billable]="0",0,[StaffTime)))
I am getting an #error for any of the first to queries the 62Billable and the 6215Billable, otherwise its great. I dont understand why they work on their own but when they are together it doesn't.

I was told I should make a union query to do this, but that doesn't seem to want to work either so I must be doing something wrong. I am actually getting a data type mismatch in criteria expression. Which doesn't make sense to me because all the fields are the same and in the same order in each query. The only thing I can think of is in the last query it ony goes off of the SerCode where the other two are a bit more complex, but still work in their own query. Help!

Here is the union query that I have that is giving me the error meesage above:

SELECT [Service Hours].KeyID, [Service Hours].StaffCode, [Service].StaffFac, [Service].StaffTime, [Service Hours].SerCode, IIf([Service Hours By]!StaffFac="62" And [Service Hours]!SerCode="90000",0,IIf([Service]!StaffFac="62" And [Service Hours]!SerCode="90001",0,[StaffTime])) AS Billable
FROM [Service Hours] LEFT JOIN [Service Code List Export] ON [Service].SerCode = [Service Code List Export].SerCode
UNION
SELECT [Service Hours].KeyID, [Service Hours].StaffCode, [Service].StaffFac, [Service Hours].StaffTime, [Service Hours].SerCode, IIf([Service Hours]!StaffFac="6215" And [Service Hours]!SerCode="90000",0,IIf([Service Hours]!FacCode="6215" And [Service Hours]!SerCode="90001",0,[StaffTime])) AS Billable
FROM [Service Hours] LEFT JOIN [Service Code List Export] ON [Service Hours].SerCode = [Service Code List Export].SerCode
UNION
SELECT [Service Hours].KeyID, [Service Hours].StaffCode, [Service Hours].StaffFac, [Service Hours].StaffTime, [Service Hours].SerCode, IIf([Service Hours]!SerCode="90002",0,[StaffTime]) AS Billable
FROM [Service Code List Export] RIGHT JOIN [Service Hours] ON [Service Code List Export].SerCode = [Service Hours].SerCode;

Any help is greatly appreciated!!
 
Last edited:

Users who are viewing this thread

Back
Top Bottom