Link to home
Start Free TrialLog in
Avatar of Monterey
Monterey

asked on

SQL If Statement in Access Query

I'm not sure what I am doing wrong, but here is my SQL Code within my Access Query:

SELECT Count(qryVendorX.[Item #]) AS [TotalItems]
FROM tblItemMaster RIGHT JOIN qryVendorX ON tblItemMaster.[Item #] = qryVendorX.[Item #]
GROUP BY qryVendorX.VendorNumber, qryVendorX.VendorName
HAVING (((qryVendorX.VendorNumber) = [forms]![frmPOTransHeader].[Vendor #]))

IIF (qryVendorX.[TotalItems])>0
SELECT qryVendorX.VendorNumber, qryVendorX.VendorName, qryVendorX.[Item #], tblItemMaster.Description, qryVendorX.[Vendor Item #]
FROM tblItemMaster RIGHT JOIN qryVendorX ON tblItemMaster.[Item #] = qryVendorX.[Item #]
GROUP BY qryVendorX.VendorNumber, qryVendorX.VendorName, qryVendorX.[Item #], tblItemMaster.Description, qryVendorX.[Vendor Item #]
HAVING (((qryVendorX.VendorNumber)=[forms]![frmPOTransHeader].[Vendor #]))

ELSE
SELECT tblItemMaster.[Item #], [Department] & "-" & [Account] & "-" & [Sub-Account] AS [G/L Account], tblItemMaster.Description
FROM tblItemMaster
ORDER BY tblItemMaster.Description;

My goal is to display a list of items specific to vendors if the information is in the Vendor Cross Reference Table.  If the Vendor is not in the cross reference table, just display all Items.  Any help is greatly appreciated.

Paula
ASKER CERTIFIED SOLUTION
Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Monterey
Monterey

ASKER

Thank you for the comment.  I didn't even think to go that way.  I am trying to populate a lookup based on the condition being met with one query and if not a different query.  I was thinking that I had to make the query do the work rather than the event on the lookup.  Thanks again.
No problem.  You are most welcome.