Access中的多个嵌套iif语句

时间:2018-12-14 21:22:44

标签: ms-access nested iif

我正在尝试使用以下类别来计算员工有权使用其服务年限的休假周数:

0-4 years = 2 weeks
5-14 years = 3 weeks
15-24 years = 4 weeks
25 and up = 5 weeks

我找到了一些示例,并尝试执行嵌套的iif语句,但始终收到错误消息。有人可以告诉我我在做什么错吗?

VacationWeeks: IIf([YearsInService]<=4, "2",
               IIf([YearsInService]is between 5 and 14, "3",
               IIf([YearsInService]is between 15 and 24, "4",
               IIf([YearsInService]>=25, "5", ”0”))))

1 个答案:

答案 0 :(得分:2)

由于代码有两个问题,您可能会收到错误消息:

  1. BETWEEN语句的语法为:

    expr [Not] Between value1 And value2
    

    您应该包括单词

    IIf([YearsInService] is between 5 and 14,
                         ^-------------------- this shouldn't be here
    
  2. 您的最终 else 参数将字符串括在“智能引号”或Unicode字符0x201C中:

    IIf
    (
        [YearsInService] >= 25,
        "5",
        ”0” <--- here
    )
    

    与标准双引号相反,后者是Unicode / ASCII字符0x0022

    IIf
    (
        [YearsInService]>=25,
        "5",
        "0"
    )
    

更正这两个问题将产生一个IIF语句,例如:

IIf
(
    [YearsInService] <= 4,
    "2",
    IIf
    (
        [YearsInService] between 5 and 14,
        "3",
        IIf
        (
            [YearsInService] between 15 and 24,
            "4",
            IIf
            (
                [YearsInService] >= 25,
                "5",
                "0"
            )
        )
    )
)

但是,您可能会发现使用SWITCH语句更易读:

Switch
(
    [YearsInService] <=  4, "2",
    [YearsInService] <= 14, "3",
    [YearsInService] <= 24, "4",
    [YearsInService] >  24, "5",
    True, "0"
)

最后一个测试表达式提供了一个包罗万象的默认值,以说明是否存在空值,并且仍然返回字符串值-我将根据您的应用程序的要求让您决定是要包含还是忽略此值。