简体   繁体   中英

If/Then in Access query

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

在此处输入图片说明

Can you help?

In MS-Access, switch is the simpler way to handle multiple conditions:

select . . .
       switch([Cardholder] Is Not Null And [Reconciler] Is Not Null And [Approver] Is Not Null, "F",
              [Cardholder] Is Not Null And [Reconciler] Is Not Null, "D",
              [Cardholder] Is Not Null And [Approver] Is Not Null, "E",
              [Reconciler] Is Not Null And [Approver] Is Not Null, "G",
              [Cardholder] Is Not Null, "A",
              [Reconciler] Is Not Null, "B",
              [Approver] Is Not Null, "C",
              1=1, "H"
             )

Since you have three fields, and each may be either Null or Not Null there are 2 3 = 8 possible cases.

The following will account for each:

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"
                        )
                    )
                )
            )
        )
    )
)

The final result "H" corresponds to the case in which all three fields are Null .

The logic could obviously be reversed to remove the Not from each statement, but I've constructed the statement based on the example you provided.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM