Crystal Report:如何在一个公式中评估多个IF语句?

8

背景

  • 我正在尝试对报表的详细信息行进行漂亮的验证。
  • 我有几个命名为Assert语句的公式,如果它们未通过测试,则返回false,如果它们通过测试,则返回true。

目标

  • 我想创建一个存储“规则违反”的数组,然后在行末显示在名为“Broken Rules”的标题下的字段中显示它们。

到目前为止我做了什么

  • 在报表标题中创建了一个数组并将其初始化为空字符串数组。
  • 创建了一个公式来评估每个规则,并递增该数组,并添加损坏的规则编号(这是针对每个规则重复的代码,没有任何花哨的东西)。这被添加到我的详细信息显示上方的抑制详细信息部分中。
  • 创建了一个公式,用于连接破坏规则数组中的元素。这是一个与我的详细字段一起显示的公式。
  • 创建了一个公式来将破坏规则数组设置为空。这个公式放在我的详细信息显示之后的抑制详细信息部分中。

问题

  • Crystal似乎不允许我找到“end if”语句。
  • 因此,似乎我只能评估一个If语句,而不能在单个公式中评估多个If语句。
  • 这意味着我不能为每个规则都做多个ifs。

示例代码

创建数组的方法(名为Init_StringVar_Array_RulesBroken的公式):

//@Init
//This goes into the report header
WhilePrintingRecords;

//initializes the array of broken rules which we'll add to during details
StringVar Array RulesBroken;
"";

以下是前三个规则评估的示例,这些规则会增加数组并添加值(这是一个名为Increment_StringVar_Array_RulesBroken的公式):

//@Increment
//Goes before the details section is displayed

//accesses the shared variable
WhilePrintingRecords;
StringVar Array RulesBroken;

//separate if statement for each assert statement

//01
if not {@Assert_01_IfCrewIsConstructionCrew_CBFlagShouldBeYesOrDirect} then
Redim Preserve RulesBroken[UBound(RulesBroken) + 1]; //extends the array to be able to hold one more item than it does currently
RulesBroken[UBound(RulesBroken)] := "01"; //adds the new string into the array

//02
if not {@Assert_02_IfCrewIsConstructionCrew_AndCBFlagIsDirect_WONumberShouldStartWithC} then
Redim Preserve RulesBroken[UBound(RulesBroken) + 1]; //extends the array to be able to hold one more item than it does currently
RulesBroken[UBound(RulesBroken)] := "02"; //adds the new string into the array

//03
if not {@Assert_03_IfCrewIsDesign_AndCBFlagIsDirect_WONumberShouldStartWithD} then
Redim Preserve RulesBroken[UBound(RulesBroken) + 1]; //extends the array to be able to hold one more item than it does currently
RulesBroken[UBound(RulesBroken)] := "03"; //adds the new string into the array

有什么想法吗?

  • Crystal Reports中是否有if/then/end if功能?
  • 如果没有,是否有解决这类问题的方法?我需要为每个公式创建多个公式并确保它们按顺序放置吗?

非常感谢您提供的任何帮助!

1个回答

12

你可以通过将if代码块用括号括起来,并用分号分隔它们的方式来实现这个最简单的代码:

//01
(
    if not {@Assert_01_IfCrewIsConstructionCrew_CBFlagShouldBeYesOrDirect} then
        Redim Preserve RulesBroken[UBound(RulesBroken) + 1];
        RulesBroken[UBound(RulesBroken)] := "01"
    else ""
);

//02
(
    if not {@Assert_02_IfCrewIsConstructionCrew_AndCBFlagIsDirect_WONumberShouldStartWithC} then
        Redim Preserve RulesBroken[UBound(RulesBroken) + 1];
        RulesBroken[UBound(RulesBroken)] := "02"
    else ""
);

//03
(
    if not {@Assert_03_IfCrewIsDesign_AndCBFlagIsDirect_WONumberShouldStartWithD} then
        Redim Preserve RulesBroken[UBound(RulesBroken) + 1];
        RulesBroken[UBound(RulesBroken)] := "03"
    else ""
);

我添加了缩进以说明 Crystal 如何解释块。


使用这段代码后,我现在收到一个“缺少'else'的单词”错误;有没有一种方法可以假装什么都不做?我应该将数组设置为自身吗? - SeanKilleen
2
否则 "" 应该可以工作。这很奇怪,我认为不需要 else - paulmelnikow
好的,谢谢你提供如此简明又详尽带有例子的答案,这非常有效! - SeanKilleen
1
刚看到这个问题并用它解决了一个类似的问题。如果你正在尝试在选择记录中使用if语句,请像上面所示将它们用括号括起来,但是不要使用分号,而是使用'and',就像你期望的那样。感谢这个解决方案! - Jackson
1
@Jackson,这对我非常有效,我已经在选择记录部分试图解决这个问题几个小时了! - HighARc
@HighARc 太好了!很高兴能帮到你。祝你在项目的其余部分好运。 - Jackson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接