4 Replies Latest reply on Jun 7, 2013 7:15 AM by AjEGfmTech

    Looping Script won't leave sub-loop?

    AjEGfmTech

      Title

      Looping Script won't leave sub-loop?

      Post

            I am trying to create a script that will create new records in a related table based on the number of 'credits' each record has in the initial table.

           Initial Table: Members (one layout, Enter_Members)
           - Field: id
           - Field: cardNumber
           - Field: credits

           Related Table: Entries (one layout, Enter_Entries)
           - Field: id
           - Field: id_member

           Members::id is related to Entries:id_member

           Following is the script...

           ----

      Go to Layout ["Enter_Members" (Members)]
      Show All Records
      Sort Records [Restore; No dialog] (sorted by 'credits')
      Go to Record/Request/Page [First]
      Loop
        Set Variable [$Credits; Value:Members::credits]
        Set Variable [$MemberID; Value:Members::id]
        Loop
          Go to Layout ["Enter_Entries" (Entries)]
          If [$Credits > 0]
            New Record/Request
            Set Field [Entries::id_member; $MemberID]
            Commit Records/Requests []
            Set Variable [$Credits; Value:$Credits - 1]
          End If
        End Loop
        Go to Layout [original layout]
        Go to Record/Request/Page [Next; Exit after last]
      End Loop
      

           ----

           For some reason the script runs through the sub-loop successfully (it creates one new record in 'Entries' for the number of credits in the first 'Members' record) but then gets stuck after that and appears to not go to the next record in the 'Members' layout . The way I was figuring this, when the sub-loop 'End Loop' occurs, the Goto Layout and Goto Next Record should fire and start the sub-loop over again, but for the next 'Members' record. Obviously, I have a flaw in my thinking.

           Any ideas or observations about what I might have done wrong would be greatly appreciated. Thanks.

        • 1. Re: Looping Script won't leave sub-loop?
          philmodjunk

               The inner loop does not have a step for exiting the loop and  so it loops forever.

               Change the inner loop to be:

           Loop
              Go to Layout ["Enter_Entries" (Entries)]
              Exit Loop If [$Credits < 0]
              New Record/Request
              Set Field [Entries::id_member; $MemberID]
              Set Variable [$Credits; Value:$Credits - 1]
           End Loop
          • 2. Re: Looping Script won't leave sub-loop?
            AjEGfmTech

                 Thanks Phil, that worked!

                 I was under the presumption that when the 'If' statement became 'False', it would proceed to the 'End If' and then the 'End Loop' and out of the sub-loop. From what you showed me, it seems that no matter what happens in the 'If', the 'End If' will NOT proceed to the 'End Loop', correct?

            • 3. Re: Looping Script won't leave sub-loop?
              philmodjunk

                   It does indeed pass control to the ENd IF step, but since that step is inside the loop, the script loops around again and control passes to the first step inside the loop.

              • 4. Re: Looping Script won't leave sub-loop?
                AjEGfmTech

                     Thanks Phil. I think I got it now. The If gets to the End If statement but the End If does not effect the loop, so the loop continues back to the top where the If starts again, still reads false, still goes to the End If but never leaves the inner loop. That makes sense, I just didn't see that before. Which is why there needs to be something that acts upon the Loop statement, which is the 'Exit Loop If' that you showed me.

                     Thanks. This makes sense now. I appreciate it.