Breaking out of loop in form

EmiliaEmilia Member Posts: 4
Is there a way to "break" out of a loop in a form? The only way that seems to work is EXIT, but I do not want to EXIT because I still want to run the rest of the code for other cases. BREAK/SKIP/QUIT do not work either. They seem to only work on reports, i.e. CurrReport.BREAK;

What is the BREAK version for forms? Something that works like CurrReport.BREAK but in forms.

Comments

  • bbrownbbrown Member Posts: 3,268
    Could you explain what you are trying to do?

    You can use a Boolean value to control a loop in code.

    Example

    MyBool := FALSE;
    REPEAT
    Your Code Here
    Set MyBool = TRUE when condition met.
    UNTIL (NEXT = 0) OR MyBool
    There are no bugs - only undocumented features.
  • EmiliaEmilia Member Posts: 4
    Let's say I have this code:

    IF PromoDet2.FINDSET THEN
    REPEAT
    CASE PromoDet2."Sales Type" OF
    PromoDet2."Sales Type"::Item :
    BEGIN
    IF SalesLine2.INSERT THEN
    CurrForm.CLOSE;
    EXIT; // I want to replace this exit with someting similar to break
    END;

    PromoDet2."Sales Type"::"Product Group" :
    BEGIN
    //code here
    END;

    PromoDet2."Sales Type"::"Shipping Agent Service":
    BEGIN
    //code here
    END;

    END;
    UNTIL PromoDet2.NEXT = 0;

    Instead of using EXIT, I want to use something similar to BREAK, so that the rest of the code in other cases can still be run. This code is done on a form, so I cannot do things like CurrReport.BREAK.

    My question is that how do you break out of that IF statement above without using EXIT?
  • SavatageSavatage Member Posts: 7,142
    Savatage wrote:
    I think your thinking about this wrong.

    I'm assuming you mean that if condition 1 is true you want it to go to the end like Pascal or something.

    The condition can only be true once and you have to loop thru for each line (if I get you correctly). Do you think that the other code will run if condition 1 is true?

    Where are you putting your code?
    This is someting you might use on a OnValidate Trigger
    CASE Type OF
      Type::" ":
        BEGIN
          //Some Code
        END;
      Type::"Account (G/L)":
        BEGIN
          //Some Code
        END;
      Type::Item:
        BEGIN
          //Some Code
        END;
      Type::Resource:
        BEGIN
          //Some Code
        END;
      Type::"Fixed Asset":
        BEGIN
          //Some Code
        END;
    END;
    

    PS Don't forget about your
    If..Then..Else's or Then Begin..Else Begin..too
    They can be very useful
  • DenSterDenSter Member Posts: 8,305
    Emilia wrote:
    .....so that the rest of the code in other cases can still be run...
    What do you mean? The whole point of a CASE statement is that only the code in the right CASE is run, and nothing else. The value of the control variable is what made it execute the code in that part of the CASE statement. Even if we had the option to break out of a CASE statement like that, it would not execute the code for any other value of the control variable.
  • EmiliaEmilia Member Posts: 4
    I have a REPEAT... UNTIL surrounding these cases. Let's say that there are 3 things that fulfil the condition of my REPEAT... UNTIL loop. I want to break out of the IF statement so that the REPEAT... UNTIL loop (and the different cases within it) can be run again for the other things that also fulfil the condition of my REPEAT... UNTIL loop.
  • SavatageSavatage Member Posts: 7,142
    I think the problem that is confusing is

    1) not know what you are trying to do

    2) looking at your code I just looked quick and saw you wanted to use case but how could a "shipping agent service" or "product group" be a sales type?

    you might be just setting this up all wrong - that's why we need to know what you are trying to accomplish
  • DenSterDenSter Member Posts: 8,305
    Consider this (and the suggestion had already been made):
    ConditionOne := FALSE;
                        IF PromoDet2.FINDSET THEN
                          REPEAT
                            CASE PromoDet2."Sales Type" OF
                              PromoDet2."Sales Type"::Item :
                                BEGIN
                                    IF SalesLine2.INSERT THEN BEGIN
                                      CurrForm.CLOSE;
                                      //EXIT; // I want to replace this exit with someting similar to break
                                      ConditionOne := TRUE;
                                    END;
                                END;
    
                              PromoDet2."Sales Type"::"Product Group" :
                                BEGIN
                                  //code here
                                END;
    
                              PromoDet2."Sales Type"::"Shipping Agent Service":
                                BEGIN
                                   //code here
                                END;
                          
                            END;
                          UNTIL (PromoDet2.NEXT = 0) OR (ConditionOne = TRUE);
    
    The only issue is that it will still execute any code you'd have after the point where you want to exit.

    So there is no keyword to exit a loop in C/AL, but you CAN make the loop stop if a condition is met, by including that condition in the UNTIL part.
  • easabanganeasabangan Member Posts: 114
    make sure you reset the value of the variable ConditionOne before using it. :lol:
    No future at CPI
Sign In or Register to comment.