AnsweredAssumed Answered

I think I got my calc/loop to work. Could it be more elegant?

Question asked by wendy_1 on Jan 11, 2010
Latest reply on Jan 12, 2010 by wendy_1

Title

I think I got my calc/loop to work. Could it be more elegant?

Post

Hello to any kind and brilliant soul who can see a better way... Background: I've done some scripting/calculations in Filemaker and have very occasionally impressed myself, but it's been a plodding experience for the most part, with less than elegant solutions, and I struggle with Ifs and Loops. Perfect example below. Using FilemakerPro 8 Advanced on a Mac with OS 10.4.11.

 

BACKGROUND:  I am capturing user input via a global (Prefix_gText), then placing that into a variable ($NewPrefix). This will eventually be used to create a new record in a table; however I need to make sure a record with this same value in the Prefix field does not already exist. If it does, I need to not let the user proceed until they change their input. (Using paused scripts and buttoned-down dialog-layouts.) Since I have an on-the-fly indexed Value List created using this field, I thought try to compare the user's input to the Value List instead of looping through all the records (on the perhaps mistaken assumption that this would be faster. Also because I just wanted to try it).  I also have a double check that they don't leave the input field blank.

 

THE QUESTION:  The script below seems to work (so far...).  But is there any cleaner way to do any of this? In particular I'm looking at the way I exit the loops--I'm just testing the reverse of my earlier test. Here's my script:

 

Set Error Capture ON

Loop

  If [IsEmpty (DD_Prefix::DD_Prefix_gText)]

    Show Custom Dialog [Prefix Field is Empty"; "Please enter a new Prefix."]

    Pause/Resume Script [Indefinitely]

  Else

    Set Variable [$NewPrefix; Value:DD_Prefix::DD_Prefix_gText]

    Loop

      If [ValueCount (FilterValues (ValueListItems ( Get ( FileName ) ; "DD_Prefix" ); $NewPrefix)) > 0

        Show Custom Dialog ["Prefix Exists"; "The prefix you entered already exists."]

        Pause/Resume Script [Indefinitely]

      End If

      Exit Loop If [ValueCount (FilterValues (ValueListItems ( Get ( FileName ) ; "DD_Prefix" ); $NewPrefix)) <1]

    End Loop

    Exit Loop If [IsEmpty (DD_Prefix::DD_Prefix_gTest) = 0]

  End If

End Loop 

Set Error Capture OFF 

 

I'm thinking that the use of another Else statement might clean things up, but I'm not sure where to stick it. So if anyone wants to tell me where to stick it... :)  Or if you wish to point out a foreseeable problem I probably haven't anticipated when taking this approach... Thx...  

Outcomes