具有特定和通配符标准的IIF声明

时间:2016-02-26 19:57:19

标签: ms-access ms-access-2010

我正在尝试编写一个查询,如果表单上的标志被关闭则返回所有结果,如果标志打开则排除特定值。我一直在搜索,但找不到我需要的确切语法。

当我在iif语句中有'Like'时(当它被评估为False时)没有返回任何结果。当我将Like移动到iif语句的外部时,我将在关闭复选框时获得预期的结果。如果打开标志,我无法获得任何结果。

有没有人知道我需要编写我的iif语句来获取所有值,如果标志被关闭,所有值只有“REPLN”,如果标志被打开?

*添加完整声明。

    SELECT MYTABLE.DPT_NUMBER, MYTABLE.VENDOR_ID AS [Vendor ID], MYTABLE.VENDOR_NAME AS [Vendor Name], Sum(MYTABLE.UNITS_ORDERED) AS [Units Ordered], Sum(MYTABLE.RECEIVED_QTY) AS [Units Received], IIf(Sum([MYTABLE]![RECEIVED_QTY])=0,0,([Units Received]/[Units Ordered])) AS [Received Fill Rate], Sum(MYTABLE.ASN_QTY) AS [ASN Ship QTY], Sum([MYTABLE]![ASN_QTY])-Sum([MYTABLE]![RECEIVED_QTY]) AS [Units In Transit], Sum([MYTABLE]![ASN_QTY])/Sum([MYTABLE]![UNITS_ORDERED]) AS [Fill Rate with In Transit], Sum([MYTABLE]![UNITS_ORDERED])-Sum([MYTABLE]![ASN_QTY]) AS [Units Open], MYTABLE.PO_TYPE
FROM MYTABLE
WHERE (((MYTABLE.PO_TYPE) Like IIf([Forms]![frmMain]![ReplnExclude]=True,([MyTable].[TYPE])<>"REPLN","*")) AND ((MYTABLE.DPT_NUMBER)=[Forms]![frmMain]![DeptNum]) AND ((MYTABLE.GROUP_ID)="RP10") AND ((MYTABLE.PO_EXP_RCT_DATE) Between [Forms]![frmMain]![StartDate] And [Forms]![frmMain]![EndDate]) AND ((MYTABLE.PO_HDR_STATUS)<>"Cancelled") AND ((MYTABLE.PO_DTL_STATUS)<>"Cancelled"))
GROUP BY MYTABLE.DPT_NUMBER, MYTABLE.VENDOR_ID, MYTABLE.VENDOR_NAME, MYTABLE.PO_TYPE
ORDER BY MYTABLE.VENDOR_NAME;

1 个答案:

答案 0 :(得分:1)

在你的where子句中,你以'WHERE(((MYTABLE.PO_TYPE)赞'开头,但没有给出你要找的内容的标准,然后用iif语句放入另一个子句。我删除了(((MYTABLE。 PO_TYPE)喜欢下面的陈述。

这里我们将AND放在语句中,因为如果它是真的我们只需要AND。否则我们只需要一个空格来移动到下一个子句。

IIf([Forms]![frmMain]![ReplnExclude] = True,([MyTable]。[TYPE])&lt;&gt;“REPLN AND”,“”))

如此完整

SELECT MYTABLE.DPT_NUMBER, MYTABLE.VENDOR_ID AS [Vendor ID], MYTABLE.VENDOR_NAME AS [Vendor Name], Sum(MYTABLE.UNITS_ORDERED) AS [Units Ordered], Sum(MYTABLE.RECEIVED_QTY) AS [Units Received], IIf(Sum([MYTABLE]![RECEIVED_QTY])=0,0,([Units Received]/[Units Ordered])) AS [Received Fill Rate], Sum(MYTABLE.ASN_QTY) AS [ASN Ship QTY], Sum([MYTABLE]![ASN_QTY])-Sum([MYTABLE]![RECEIVED_QTY]) AS [Units In Transit], Sum([MYTABLE]![ASN_QTY])/Sum([MYTABLE]![UNITS_ORDERED]) AS [Fill Rate with In Transit], Sum([MYTABLE]![UNITS_ORDERED])-Sum([MYTABLE]![ASN_QTY]) AS [Units Open], MYTABLE.PO_TYPE
FROM MYTABLE

WHERE IIf([Forms]![frmMain]![ReplnExclude]=True,([MyTable].[TYPE])<>"REPLN AND",""))  ((MYTABLE.DPT_NUMBER)=[Forms]![frmMain]![DeptNum]) AND ((MYTABLE.GROUP_ID)="RP10") AND ((MYTABLE.PO_EXP_RCT_DATE) Between [Forms]![frmMain]![StartDate] And [Forms]![frmMain]![EndDate]) AND ((MYTABLE.PO_HDR_STATUS)<>"Cancelled") AND ((MYTABLE.PO_DTL_STATUS)<>"Cancelled"))
GROUP BY MYTABLE.DPT_NUMBER, MYTABLE.VENDOR_ID, MYTABLE.VENDOR_NAME, MYTABLE.PO_TYPE
ORDER BY MYTABLE.VENDOR_NAME