简体   繁体   English

MS Access中IIf语句的替代方法

[英]Alternative to IIf statement in MS Access

I've got an IIf statement that should do what I want, but its too complex so I need an alternative way of writing this code. 我有一个IIF语句应该做我想做的,但它太复杂了,所以我需要编写这些代码的另一种方式。 I've tried a switch statement but have the same issue. 我已经尝试过switch语句,但是有相同的问题。 The purpose of the code is difficult to explain, but I'll try to keep it simple, as I've found the context is important while searching for solutions. 代码的目的很难解释,但我会尝试使其简单,因为我发现在寻找解决方案时上下文很重要。

Basically, I'm using a query to output a datasheet that can be used as a mock matrix. 基本上,我使用查询来输出可以用作模拟矩阵的数据表。 It needs to be able to be customized, ie 5x5, 7x7, 10x10, etc. and the values displayed need to change to reflect the dimensions. 它需要能够进行自定义,即5x5、7x7、10x10等,并且显示的值需要更改以反映尺寸。 One of the tables the query pulls from is just number values that I use the sum of to determine the output of that field. 查询从中提取的表之一只是数值,我使用这些数值的总和来确定该字段的输出。 There is also a field labeled "[Matrix Size]" that determines the dimensions. 还有一个标记为“ [Matrix Size]”的字段,用于确定尺寸。 Eventually, this will also dictate what information gets displayed based on those dimensions, but I haven't gotten that far yet. 最终,这还将决定根据这些维度显示哪些信息,但是我还没有得到那么多。 The overly complex IIf statement(s) is as follows. 过于复杂的IIf语句如下。

    IIf([Matrix Size]<=0," ",
        IIf([Impact]=0,"1",
            IIf([Impact]+[Likelihood]=2,"Low",
                IIf([Matrix Size]<2," ",
                   IIf([Impact]+[Likelihood]=3,"Low", 
                       IIf([Matrix Size]<3," ",
                          IIf([Impact]+[Likelihood]=4,"Low",
                             IIf([Matrix Size]<4," ",
                                IIf([Impact]+[Likelihood]=5,"Low",
                                   IIf([Matrix Size]<5," ",
                                       IIf([Impact]+[Likelihood]=6,"Low",
                                          IIf([Matrix Size]<6," ",
                                             IIf([Impact]+[Likelihood]=7,"Low",
                                                IIf([Matriz Size]<7," ",
                                                  IIf([Impact]+[Likelihood]=8,"Low","Error")))))))))))))))

If anyone knows of a way to simplify this/an alternate way of going about it that wont be too complex for access, please let me know. 如果有人知道简化此方法的另一种方式,并且访问不会太复杂,请告诉我。 Any help is appreciated. 任何帮助表示赞赏。

For Clarity, if needed: 为了清晰起见,如果需要:

The second IIf is used to set the first row to be "1", to act as one of the numerical values along a horizontal row. 第二个IIf用于将第一行设置为“ 1”,以用作沿水平行的数值之一。 The impact+likelihood statements determine if the field should display a value of "Low","Medium", or "High", but for now "Low" is just a placeholder, and the matrix size statements determine if it should be blank or not based on desired dimensions. Impact + likelihood语句确定该字段应显示的值是“ Low”,“ Medium”还是“ High”,但目前“ Low”只是一个占位符,矩阵大小语句确定该字段应为空白还是空白并非基于所需的尺寸。

The alternative you are looking for is switch() : 您正在寻找的替代方法是switch()

switch([Matrix Size] <= 0, " ",
       [Impact] = 0, "1",
       [Impact] + [Likelihood] = 2, "Low",
       . . .
       "Error"
      )  -- only one paren needed!

Unfortunately, your conditions are a mixture based on the three columns, so a lookup table would be a little bit cumbersome. 不幸的是,您的情况是基于三列的混合情况,因此查找表会有些麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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