If/Then in Access query

时间:2018-02-26 17:44:42

标签: sql ms-access if-this-then-that

I need some help constructing an If/Then query in Access. I want the New Comments field to populate based on the Cardholder, Approver, and Reconciler columns:

If only Cardholder is filled: New Comments field = A
If only Reconciler is filled: New Comments field = B
If only Approver is filled: New Comments field = C
If Cardholder & Reconciler filled:  New Comments field =D
If Cardholder & Approver filled: New Comments field = E
If Cardholder & Reconciler & Approver filled: New Comments field = F
If Reconciler & Approver filled: New Comments field = G

enter image description here

Can you help?

1 个答案:

答案 0 :(得分:1)

由于您有三个字段,每个字段可能是NullNot Null,因此有2个 3 = 8个可能的情况。

以下各项将解释:

NewComments: 
IIf([Cardholder] Is Not Null And [Reconciler] Is Not Null And [Approver] Is Not Null,"F",
    IIf([Cardholder] Is Not Null And [Reconciler] Is Not Null,"D",
        IIf([Cardholder] Is Not Null And [Approver] Is Not Null,"E",
            IIf([Reconciler] Is Not Null And [Approver] Is Not Null,"G",
                IIf([Cardholder] Is Not Null,"A",
                    IIf([Reconciler] Is Not Null,"B",
                        IIf([Approver] Is Not Null,"C",
                            "H"
                        )
                    )
                )
            )
        )
    )
)

最终结果"H"对应于所有三个字段均为Null的情况。

显然可以颠倒逻辑来从每个语句中删除Not,但我根据您提供的示例构建了语句。