3 Replies Latest reply on Jan 12, 2010 2:05 PM by wendy_1

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

    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...  

        • 1. Re: I think I got my calc/loop to work. Could it be more elegant?
          wendy_1
            

          Well, it would certainly be more elegant without the emoticons, which didn't show up prior to posting.  Uh, when reading this script, replace them with

          : (colon) D (capital D)

           

          Shees. 

          • 2. Re: I think I got my calc/loop to work. Could it be more elegant?
            philmodjunk
              

            Too kill off the emoticons, you can edit your message by inserting a space after the second colon. You can even do this after you've posted the mesage.

             

            I think you're overcomplicating this a lot.

             

            Here's another approach that's a bit simpler.

             

            Define a relationship between your global field and the table where you want to check to see if this value has already been entered. If this is the same table, no big deal. You can set up a self-join to the same table. (Drag from the global field to the specific data field in the same Table Occurrence box and you'll get a new table occurrence box for that table.)

             

            Then the simple statment IsEmpty(RelatedTable:: Datafield) will be true if the value was never previously used.

             

            Now you can check for this value with a single line in your script:

            If [isempty(Yourrelatedtable::YourDatafield)]

               #put steps here if that you want if the value doesn't exist

            Else

              #put steps here that you want if the value does exist

            End If

            • 3. Re: I think I got my calc/loop to work. Could it be more elegant?
              wendy_1
                

              Well then.

               

              Thank you for supplying the Queen Mary alternative.  I will now proceed with swamping my dingy rowboat.

               

              (...Oh well. I guess I did learn something from all that rowing!)

               

              I have a tendency to overlook potential "relationship-based" answers in Filemaker.  Before I get involved in heavy calculation and scripting, I have to try and remember to stop and consider whether a simple relationship will solve the problem.

               

              Thank you.