Solved

SQL If Statement in Access Query

Posted on 2008-10-23
3
647 Views
Last Modified: 2013-11-27
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
0
Comment
Question by:Monterey
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 22791981
You would have to use this syntax in MS Access:

IIF(condition, true-statement, false-statement)

If you are trying to display a full different query based on some condition, you may be better off moving to VBA code and then it is of syntax:

IF condition THEN
    'true-statement
ELSE
    'false-statement
END IF
0
 

Author Comment

by:Monterey
ID: 22843281
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.
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22844518
No problem.  You are most welcome.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question