1 2 Previous Next 26 Replies Latest reply on Dec 22, 2016 6:55 AM by leish2

    How to write scripts to CHECK or UNCHECK checkbox option

    jsml

      Please do not say "replace field contents"!

       

      Say I have a field with numerous checkboxes as options. I want to CHECK or UNCHECK a certain checkbox for all the fields in my found set. I do NOT want to replace.

       

      This is the script I have now for check. But it replaces rather than ADDS a checkmark.

      Screen Shot 2016-12-07 at 8.28.31 PM.png

       

      This is the script that i have now for uncheck. I am not sure what it does and my brain is scrambled.

      Screen Shot 2016-12-07 at 8.27.54 PM.png

       

       

      FYI: I am using FileMaker Pro 14 and have very little understanding of scripts so you have to really explain things to me. I wrote both these scripts so long ago, I can't even follow them. I do have experience with functions, but mostly in Excel.

       

      I believe philmodjunk tried to help me years ago, but for some reason, the forum has lost the links to all the posts I was following or participated in.

        • 1. Re: How to write scripts to CHECK or UNCHECK checkbox option
          beverly

          jsml, it may help to understand the "checkbox" formatting for a field.

           

          1. a field is a field is a field, (if TEXT) it just has characters.

          2. a field on a layout which has been formatted with checkboxes is still a text field, but has "controls" which toggle on & off items in a value list assigned to it in layout mode via Inspector.

          3. if you have the same field on the layout and format one with checkboxes and one with no formatting, you will see:

           

          • the "plain" field will populate (unpopulate) with the value list items and in the ORDER with which you check in the formatted field

          • the "plain" field will appear to be a LIST (return-delimited/separated) of items that match what is in the formatted field - and you would be correct, because that is exactly what is happening.

          • if you add values in the plain field (as you would when the script is run), it must be an append and must have the return also added to the value item. if the item matches something in that value list, the formatted field will show the "checked"

          • if you remove a value item in the LIST, you must also remove the return

           

          Do some manual testing of the above so that you understand what you are doing to change the field and then make the same steps to script the same method.

           

          beverly

          • 2. Re: How to write scripts to CHECK or UNCHECK checkbox option
            Stephen Huston

            You can use the Substitute function to replace the chosen option with null, then also test for consecutive Returns.

            • 3. Re: How to write scripts to CHECK or UNCHECK checkbox option
              philmodjunk

              Replace Field contents can be used to do what you want, but not how you've used it here.

               

              Say you have a check box field with two check box values: "A" and "B".

               

              The following expression used with Replace Field Contents "calculation" option will select "B" while leaving any selected "A" values unchanged:

               

              If ( IsEmpty ( FilterValues ( "B" ; CheckBoxField ) ) ; List ( CheckBoxField ; "B" ) ; CheckBoxField )

              • 4. Re: How to write scripts to CHECK or UNCHECK checkbox option
                beverly

                while, not "pretty", extra returns do not affect the appearance in the checkbox-formatted version of the field.

                beverly

                • 5. Re: How to write scripts to CHECK or UNCHECK checkbox option
                  Stephen Huston

                  Agrred. Extra returns aren't a big deal in formatting checkboxes, but they can be a data issue in other uses of some field contents.

                  • 6. Re: How to write scripts to CHECK or UNCHECK checkbox option
                    jsml

                    Hi, Beverly!

                     

                    I do understand how a checkbox works and what you explained in the plain vs. checkbox views of a field.

                     

                    What I do NOT understand is the scripting itself. What you're telling me to do is what I don't know how to do. I don't know how to use the script language to APPEND a value. I have tested the script I provided for adding -- as I said, it replaces the field, it is not appending. The removal script doesn't work either.

                     

                    Here's what I understand as a basic outline what the scripts need to do...I just don't know how to script them...

                     

                    FOR ADDING

                    1. Check to see if a field already contains a certain value (the checkbox / list value in question).
                    2. If it IS included, move on to next record.
                    3. Otherwise, it means it is NOT "checked", then add the value with a paragraph return after (as long as I have put paragraph returns after all of the list values, then I shouldn't need one before AND after, I believe), then move on to the next record.

                     

                    FOR REMOVING

                    1. Check to see if a field already contains a certain value (the checkbox / list value in question).
                    2. If it IS included, remove it AND a line break after (again, assuming my value list always has a line break after each item?). Then move on to the next record.
                    3. Otherwise, if it is NOT included, just move on to the next record.
                    • 7. Re: How to write scripts to CHECK or UNCHECK checkbox option
                      jsml

                      I don't want to replace. I want to append or remove.

                      • 8. Re: How to write scripts to CHECK or UNCHECK checkbox option
                        jsml

                        I don't know what those words together mean!

                        • 9. Re: How to write scripts to CHECK or UNCHECK checkbox option
                          philmodjunk

                          The following calculation removes value "B" (clears the checkbox) while leaving any other values unmodified and can also be used with Replace Field Contents:

                           

                          Let ( [ TextString = ¶ & checkBoxField & ¶ ;

                                    RemovedString = Substitute ( TextString ; ¶ & "B" & ¶ ; ¶ ) ] ;

                                    Middle ( RemovedString ; 2 ; Length ( RemovedString ) - 2 )

                                  )

                           

                          The Middle function removes the extra ¶ characters at beginning and end. This is not needed to make the check box formatting work, but sometimes a field is used in one context with an edit box format and another as a check box format and so I make it a habit to remove the extra returns that would otherwise build up in this field with repeated uses of this replace.

                          • 10. Re: How to write scripts to CHECK or UNCHECK checkbox option
                            beverly

                            Great! look at FilterValues() for testing IF a value already exists in your field, rather than PatternCount()

                            I'm not quite sure what is breaking. are you saying it always clears the field and only sets the desired value?

                            beverly

                            • 11. Re: How to write scripts to CHECK or UNCHECK checkbox option
                              jsml

                              Is TextString the value I want?

                              And checkBoxField the field?

                               

                              If so, why would the field be surrounded by paragraph returns?

                               

                              Wouldn't it need to be

                               

                              Let ( [ checkBoxField = ¶ & TextString & ¶ ;

                                        RemovedString = Substitute ( checkBoxField ; ¶ & "B" & ¶ ; ¶ ) ] ;

                                        Middle ( RemovedString ; 2 ; Length ( RemovedString ) - 2 )

                                      )

                               

                              ?

                               

                              Also I just really don't understand this script or how to dissect it, but I can look up each function and try to understand.

                               

                              How would you then append?

                              philmodjunk wrote:

                               

                              The following calculation removes value "B" (clears the checkbox) while leaving any other values unmodified and can also be used with Replace Field Contents:

                               

                              Let ( [ TextString = ¶ & checkBoxField & ¶ ;

                              RemovedString = Substitute ( TextString ; ¶ & "B" & ¶ ; ¶ ) ] ;

                              Middle ( RemovedString ; 2 ; Length ( RemovedString ) - 2 )

                              )

                               

                              The Middle function removes the extra ¶ characters at beginning and end. This is not needed to make the check box formatting work, but sometimes a field is used in one context with an edit box format and another as a check box format and so I make it a habit to remove the extra returns that would otherwise build up in this field with repeated uses of this replace.

                              • 12. Re: How to write scripts to CHECK or UNCHECK checkbox option
                                jsml

                                Yes, @beverly, it completely replaces the field contents instead of appending it.

                                • 13. Re: How to write scripts to CHECK or UNCHECK checkbox option
                                  jsml

                                  I hate to ask such detailed questions, but can someone ID the functions that I need to be doing for each of the operations I know need to happen? If you can do that, I can try to figure out the rest by looking up each functions syntax, etc.

                                   

                                  philmodjunk beverly StephenHuston

                                   

                                  FOR ADDING

                                  1. Check to see if a field already contains a certain value (the checkbox / list value in question).
                                  2. If it IS included, move on to next record.
                                  3. Otherwise, it means it is NOT "checked", then add the value with a paragraph return after (as long as I have put paragraph returns after all of the list values, then I shouldn't need one before AND after, I believe), then move on to the next record.

                                   

                                  FOR REMOVING

                                  1. Check to see if a field already contains a certain value (the checkbox / list value in question).
                                  2. If it IS included, remove it AND a line break after (again, assuming my value list always has a line break after each item?). Then move on to the next record.
                                  3. Otherwise, if it is NOT included, just move on to the next record.
                                  • 14. Re: How to write scripts to CHECK or UNCHECK checkbox option
                                    philmodjunk

                                    textString is the current contents of the check box field with a return put before the first character and a return added after the last character. The calculation relies on them as value separators so that the substitute function will work consistently.

                                     

                                    WIthout using the returns like this, there can be cases where substitute might corrupt the text in the field by substituting values that should not be substituted.

                                     

                                    Say your A and B values are Apple and AppleSauce. Without the use of the returns like this, Trying to clear the Apple check box will also remove Apple from AppleSauce and this could leave only "Sauce" in the field--a value that matches neither check box value and so both check boxes are cleared while leaving data 'hidden' in the field that will only be visible if you remove the check box formatting and check the contents using an edit box format.

                                    Thus the calculation puts returns at the beginning and end, then removes them in the final step after the value has been removed from the list so that it "tidies up" after itself.

                                     

                                    You might want to put two copies of your field on a layout. Give one copy the check box format and the other the Edit Box format. Then observe what happens when you manually select/clear check boxes and also to test what happens when you use a script to do the same.

                                    1 2 Previous Next