6 Replies Latest reply on Mar 11, 2010 8:44 AM by mrvodka

    Adding Variables to a script

    ThomasStout

      Title

      Adding Variables to a script

      Post

      Hello,

       

      I'm just learing FM 10 after having used FM 6 for years.  I'm on a Mac, runnning Tiger, FM Adv 10.  I have the following script:

       

      Find this Week

      Go to Layout [ “Client Name/Time” (Schedule) ]
      Enter Find Mode [ Specified Find Requests: Find Records; Criteria: Schedule::ThisWeek: “...”
      Omit Records; Criteria: Schedule::Schedule: “avail”
      Omit Records; Criteria: Schedule::Schedule: “e-bike” ]
      [ Restore ]
      Set Field [ Schedule::ThisWeek; WeekOfYear ( Get (CurrentDate) ) & Year ( Get (CurrentDate) ) ]
      Perform Find [  ]
      Sort Records [ Specified Sort Order: Schedule::Planner Date; ascending
      Schedule::SchTime; ascending ]
      [ Restore; No dialog ]

       

      My understanding is that I can add variables to a script so that if I want to Find the week previous or next, I can use this script as a base and get different weeks using variables.  Is this true?  If so, could someone explain how to set that up?  And of course, if I'm on the wrong track, please let me know that too.

       

      Thanks,

      Thomas

       





        • 1. Re: Adding Variables to a script
          philmodjunk

          Let's say you store a week number in a field, Week. (0 = this week, -1 = last week, 1 = next week, ... etc.)

           

          If this is a global field you don't need variables and you could have done it with filemaker 6. If it's a local variable, you can put the value in a variable prior to entering find mode.

           

          Set variable [$Wks ; Value : YourTable::Week]

          Enter Find Mode [ Restore ]

          Set Field [ Schedule::ThisWeek; WeekOfYear ( Get (CurrentDate) + ( 7 * $Wks ) ) & Year ( Get (CurrentDate) + ( 7 * $Wks) ]

           

          (With a global field, you'd just use it in place of $Wks in the set field instruction.)

           

          • 2. Re: Adding Variables to a script
            comment_1

             


            Thomas Stout wrote:

            My understanding is that I can add variables to a script so that if I want to Find the week previous or next, I can use this script as a base and get different weeks using variables.


             

             

            I don't think variables are what you are looking for - it seems a script parameter would be more suitable here.

            To take a simpler example:

             

            Enter Find Mode [ ]

            Set Field [ Table::Datefield ; Get (CurrentDate) + Get (ScriptParameter) ]

            Perform Find [ ]

             

            will find today's records when called by a button with script parameter 0 (or none), yesterday's records when the button has a script parameter of -1, etc.


            • 3. Re: Adding Variables to a script
              ThomasStout

              Okay, I understand I could have done this with a global field in FM 6, however, I'm trying to learn how to use variables and I thought this might be a good place to try.

               

              You wrote:

              Set Field [ Schedule::ThisWeek; WeekOfYear ( Get (CurrentDate) + ( 7 * $Wks ) ) & Year ( Get (CurrentDate) + ( 7 * $Wks) ]

               

              If Current Date = 3/9/10, then WeekOfYear = 11, Year = 2010

              Set Field [ Schedule::ThisWeek; WeekOfYear ( 11 + ( 7 * $Wks ) ) & Year ( 2010 + ( 7 * $Wks) ]

               

              Where $Wks = 0 for this week, 1 for next week and -1 for last week.

              Then,

              This week would be: WeekOfYear (11 + 0),  Year (2010 + 0)

              Next week would be: WeekOfYear (11 + 7),  Year (2010 + 7)

              Last week would be: WeekOfYear (11 - 7),  Year (2010 - 7)

               

              How does that work?

               

              I must be misunderstanding something,

              Thomas

               

               

              • 4. Re: Adding Variables to a script
                philmodjunk

                Note that I am adding/subtracting multiples of 7 days from the date instead of the week of the year or year.

                 

                I could have written it this way:

                 

                Set Field [ Schedule::ThisWeek; Let ( theDate = Get ( CurrentDate ) + (7 * $Wks) ) ; WeekOfYear ( theDate ) & Year ( theDate ) ]

                 

                Perhaps that's a bit easier to understand?

                 

                (Dates are treated as simple integers counting the number of days when they are used in calculations. )

                 

                As comment posted, this information could also be passed to the script as a parameter.

                • 5. Re: Adding Variables to a script
                  ThomasStout

                  I can see now that I wasn't clear enough in my first question, sorry about that.

                   

                  I believe that what I'd like to do is have 3 buttons, one in the middle that finds this week, one on the left that goes back one week each time it's clicked and one on the right that goes forward one week each time it's clicked.

                   

                  Is this best accomplished with Script Parameters?

                   

                  Thanks,

                  Thomas

                   

                  • 6. Re: Adding Variables to a script
                    mrvodka

                    I would handle it this way.

                     

                    Create a global date field ( gDate). You will need this to be the constant so that you keep track of the week selected.

                     

                    3 Script Parameters for the 3 buttons.

                     

                    PreviousButton script parameter: gDate - 7

                    CurrentButton script parameter: Get ( CurrentDate )

                    NextButton script parameter: gDate + 7

                     

                    The iniitial value of gDate should be set to Get ( CurrentDate ) with a script such as an opening script.

                     

                     

                    Then your script woud simply be:

                     

                    Set Field [ gDate; Get ( ScriptParameter ) ]

                    Enter Find Mode [ ]

                    Set Field [ YourDateField; Let ( d = gDate; d - DayOfWeek ( d ) + 1 & ".." & d + 7 - DayOfWeek ( d ) ) ]

                    Perform Find [ ]