6 Replies Latest reply on Nov 15, 2013 2:46 PM by JimBessette

    Script to set a checkbox to a value.

    JimBessette

      Title

      Script to set a checkbox to a value.

      Post

           I have one field that is using values in checkboxes.   I want to create a script that will "set" one of the checkboxes, without altering the others.  I can easily do this with radio buttons with Set Field.  But I know that checkboxes are stored as (for lack of better terminology right now (array?)) a list of values that are set.   I don't know if there's a relatively simple script to set one box without unsetting the rest.  I'm sure I can figure out a rather long script to get all the values, and determine what is set and not, and add the new one in.  Seems like there would be an easier way to do this with some function that already exists.  I'm just not familiar enough with them to know if there is.

           Thanks,

           Jim

        • 1. Re: Script to set a checkbox to a value.
          philmodjunk

               Set field [ yourtable::checkboxfield ; list ( yourtable::checkboxfield ; "newvalue" ) ]

          • 2. Re: Script to set a checkbox to a value.
            JimBessette

                 Thanks AGAIN Phil.   I doubt I'd have figured that one out without a lot of reading….somewhere.  I searched around this forum but didn't find it.  It's almost hard to believe a "new" question can be asked here.  This works, with one caveat.  If the field is already set, it ADDS the same field to the list.  So, now there are duplicate values in my list.   I tried this:  If  List ( Names::Type ; "Rider" )  = ""  …but that doesn't help me.  I guess now I'm looking for the function that says something like   If Rider exists in Names::Type Then <do nothing>  Else <your script line>.   Again, nothing jumps out at me looking through the existing functions.

                 Jim

            • 3. Re: Script to set a checkbox to a value.
              philmodjunk

                   In many cases, the duplicate values won't cause any problems. A checkbox format on the field, for example will continue to look and function correctly even though there are multiple copies of the same value listed in the field.

                   Here is a more sophisticated calculation, that only adds the value if it is not already selected:

                   Set Field [ YourTable::CheckBoxField ;
                            IF ( IsEmpty ( FilterValues ( YourTable::CheckboxField ; "NewValue" ) ) ; List ( YourTable::CheckboxField; "newvalue") ; YourTable::CheckBoxField) ]

              • 4. Re: Script to set a checkbox to a value.
                JimBessette

                     That does the trick.   You're right about it looking the same.  Only weirdness is if someone manually unchecked the field, it doesn't APPEAR to uncheck.  It is, of course, removing the last entered value in the array, but leaves the first.  This version works like a charm.   Just trying to make sure I understand this statement.   Is that  IF  portion a shortened form of an if-else?   I.e. If "NewValue" is not in the list,  set to new value, otherwise just set it to what it is?

                     Thanks.

                • 5. Re: Script to set a checkbox to a value.
                  philmodjunk

                       It's an If function and you can look it up in FileMaker help.

                       If ( boolean Expression ; True Expression ; False Expression )

                       If boolean Expression evaluates as True or a number value other than 0 or null, the True Expression is evaluated to return a value. If the boolean expression evaluates as False, null or a zero, the False expression is evaluated to return a value. In my last example:

                       IF ( IsEmpty ( FilterValues ( YourTable::CheckboxField ; "NewValue" ) ) ; List ( YourTable::CheckboxField; "newvalue") ; YourTable::CheckBoxField)

                       The boolean expression is red, the True expression is green and the false expression is blue.

                  • 6. Re: Script to set a checkbox to a value.
                    JimBessette

                         Right.  Exactly what I thought.