1 2 Previous Next 15 Replies Latest reply on Jul 31, 2017 5:17 AM by beverly

    Can a variable be dynamically accessed?

    PaulSeabright

      I have a number of variables called $id1 through to $id8

       

      I would like to refer to these variables in a loop and have tried various solutions, but can't find the right one!.

       

      set Variable [$index; value:8]

      loop

           new record/request

           set Field [SerialNumber::SerialNumber; $id(and the index number)

           }

           } LOADS MORE SET FIELD STEPS.

           }

           set Variable [$index; Value:$index - 1

           exit loop if [$index = 0]

      end loop

       

      Windows 7 64 bit, FMPA 15.0.3.305

       

      Any help greatly received!

        • 1. Re: Can a variable be dynamically accessed?
          user19752

               set Field [SerialNumber::SerialNumber; Evaluate ( "$id" & $index )]

           

          You can use index of variable at "repetition" of set variable (like repeated field), may be easier

          $id[$index]

          but not supported on some context...

          2 of 2 people found this helpful
          • 2. Re: Can a variable be dynamically accessed?
            Malcolm

            I agree with user19752, using the variable repetition number seems like the easiest way to handle this.

             

            Instead of naming your variables $id1, $id2, $id3. .... Use one variable $id and set different values to repetition 1, 2, 3, ... and so on.

             

            malcolm

            2 of 2 people found this helpful
            • 3. Re: Can a variable be dynamically accessed?
              beverly

              Yes, but it may be difficult to remember too many indexed repetitions. Try "Named Buckets":

              Beverly

              • 4. Re: Can a variable be dynamically accessed?
                BruceRobertson

                "I have a number of variables called $id1 through to $id8"

                Why?

                How did those variables get created?

                Seems to me that process  could be modified to create $ID[ n ] where n is 1 to 8.

                • 5. Re: Can a variable be dynamically accessed?
                  fmpdude

                  So, you're talking about having an indexed variable, like an "Array"?

                  • 6. Re: Can a variable be dynamically accessed?
                    PaulSeabright

                    Thanks for the suggestions folks.

                     

                    I like the idea of repeating values, though in the past I've steered away from them as I had the impression of them being 'bad', and to be avoided at all costs.

                     

                    Below is an extract of the script I'm working on.  The user is required to complete a form documenting serial numbers for the major sub-assemblies of the product we make here.

                     

                    These serial numbers are then written to a serial number 'history' table so that I can see, for any given sub-assembly, when it left the factory, when it was installed, when it was returned for repair etc.

                     

                    I am doing that now, with the script steps in blue. However... following on from recently attending an excellent course run by David Head of uLearnIT where he suggested trying different methods to achieve the same result in order to develop and improve your programming knowledge, and seeing several blocks of repeating code, I thought that this must be a good candidate!

                     

                    Hence the question and was initially 'how to replace $id1, $id2 etc with $id(incrementing value)'

                     

                    Set Variable [$id1; Value:Machine::TopGearbox_id]

                    Set Variable [$part1; Value:TopGearbox::PartNumber]

                    Set Variable [$id2; Value:Machine::LowerGearbox_id]

                    Set Variable [$part2; Value:LowerGearbox::PartNumber]

                    Set Variable [$id3; Value:Machine::Cabinet_id]

                    Set Variable [$part3; Value:Cabinet::PartNumber]

                    Set Variable [$id4; Value:Machine::ControlBox_id]

                    Set Variable [$part4; Value:ControlBox::PartNumber]

                    Set Variable [$id5; Value:Machine::Engine_id]

                    Set Variable [$part5; Value:Engines::PartNumber]

                    Set Variable [$id6; Value:Machine::BladeType_id]

                    Set Variable [$part6; Value:Machine::BladeType]

                    Set Variable [$id7; Value:Machine::Clutch_id]

                    Set Variable [$part7; Value:Clutch::PartNumber]

                    Set Variable [$id8; Value:Machine::BaseRadio_id]

                    Set Variable [$part8; Value:Base Radio::PartNumber]

                    Set Variable [$id9; Value:Machine::CanopyRadio_id]

                    Set Variable [$part9; Value:Canopy Radio::PartNumber]

                     

                    Close Window [ Current Window ]

                    Go to Layout [“New Job | iPad” (ServiceJobs)]

                    Perform Quick Find [ServiceJobs::__pk_ServiceJob_id = $ServiceJob_id]

                    Go to Layout [“SerialNumber” (SerialNumber)]

                    #  Add Serial numbers to Serial number table.

                     

                    Set Variable [$index; Value:9]

                     

                    Loop

                        New Record/Request

                        Set Field [SerialNumber::_fk_FittedTo_Tower_id; $NewTower_id]

                        Set Field [SerialNumber::_fk_ServiceJob_id; $serviceJob_id]

                        Set Field [SerialNumber::SerialNumber; Evaluate ( "$id" & $index)] // this doesn't work

                        Set Field [SerialNumber::Status; "Installed"]

                        Set Field [SerialNumber::_fk_Part_id; $&"part"&$index] // neither does this!

                        Set Variable [$index; Value:$index - 1]

                         Exit Loop If [$index = 0]

                    End Loop

                     

                    New Record/Request

                    Set Field [SerialNumber::_fk_FittedTo_Tower_id; $NewTower_id]

                    Set Field [SerialNumber::_fk_ServiceJob_id; $serviceJob_id]

                    Set Field [SerialNumber::SerialNumber; $id1]

                    Set Field [SerialNumber::Status; "Installed"]

                    Set Field [SerialNumber::_fk_Part_id; $part1]

                     

                    New Record/Request

                    Set Field [SerialNumber::_fk_FittedTo_Tower_id; $NewTower_id]

                    Set Field [SerialNumber::_fk_ServiceJob_id; $serviceJob_id]

                    Set Field [SerialNumber::SerialNumber; $id2]

                    Set Field [SerialNumber::Status; "Installed"]

                    Set Field [SerialNumber::_fk_Part_id; $part2]

                     

                    Repeated another 7 times.....

                    • 7. Re: Can a variable be dynamically accessed?
                      philmodjunk

                      A repeating variable is not the same as a repeating field. I think that you'll find that most advice against a repeating entity refers to a repeating field, not a variable.

                      1 of 1 people found this helpful
                      • 8. Re: Can a variable be dynamically accessed?
                        BruceRobertson

                        Having such specific field names suggests a serious design problem.

                        What kind of product or service are you tracking?

                        Does the job always consist of strictly these parts?

                         

                        Machine::TopGearbox_id

                        TopGearbox::PartNumber

                         

                        Machine::LowerGearbox_id

                        LowerGearbox::PartNumber

                         

                        Machine::Cabinet_id

                        Cabinet::PartNumber

                         

                        Machine::ControlBox_id

                        ControlBox::PartNumber

                         

                        Machine::Engine_id

                        Engines::PartNumber

                         

                        Machine::BladeType_id

                        Machine::BladeType

                         

                        Machine::Clutch_id

                        Clutch::PartNumber

                         

                        Machine::BaseRadio_id

                        Base Radio::PartNumber

                         

                        Machine::CanopyRadio_id

                        Canopy Radio::PartNumber

                        • 9. Re: Can a variable be dynamically accessed?
                          PaulSeabright

                          Hi Bruce,

                          Yes, they do.

                          We're a kinda one horse company - different colour horses thought .

                          99% of the product we manufacture will have all of the components listed, the remaining 1% will have a subset of those components.

                          There is no intention to sell this solution to any other outfit.

                           

                          I'm not sure I understand why specific field names are a serious design flaw - how would you suggest going about it?

                          • 10. Re: Can a variable be dynamically accessed?
                            PaulSeabright

                            Thanks Phil,

                             

                            I'd not made that differentiation before.  Thanks for pointing it out.

                            • 11. Re: Can a variable be dynamically accessed?
                              BruceRobertson

                              I will expand on it more latter. And others may jump in before that.

                              But you would have NONE of the problems you are now trying to deal with if you had a normal (normalized) design. It's a disaster.

                              • 12. Re: Can a variable be dynamically accessed?
                                BruceRobertson

                                I really do hope others will jump in here.

                                This is all about some very important, basic principles of database design: normalization.

                                Explanations and lessons on this subject have been presented here at great length by many.

                                It is really cool and powerful once you begin to grasp it.

                                 

                                I suggest you do a search here for BOM (bill of materials) and Assemblies

                                Some results you'll find include the following.

                                 

                                Re: BOM for sub-assemblies

                                 

                                Another BOM Tree - Free

                                • 13. Re: Can a variable be dynamically accessed?
                                  PaulSeabright

                                  Thanks for the link Beverly.

                                   

                                  I must admit that a lot most of Wims work was way beyond my current level    Man, he know some stuff!

                                  • 14. Re: Can a variable be dynamically accessed?
                                    user19752

                                    Using repetition for variable,

                                    Set Variable [$id; Value:Machine::TopGearbox_id] //repetition 1 is default value, not appear in list of steps

                                    Set Variable [$part; Value:TopGearbox::PartNumber]

                                    Set Variable [$id[2]; Value:Machine::LowerGearbox_id]

                                    Set Variable [$part[2]; Value:LowerGearbox::PartNumber]

                                    ...

                                    Set Field [SerialNumber::SerialNumber; $id[$index]] // this would work

                                    Set Field [SerialNumber::Status; "Installed"]

                                    Set Field [SerialNumber::_fk_Part_id; $part[$index]] // also does this!

                                    Set Variable [$index; Value:$index - 1]

                                    Exit Loop If [$index = 0]

                                    End Loop

                                    And your loop started at max value (9), isn't this problem?

                                    1 2 Previous Next