11 Replies Latest reply on Nov 14, 2014 12:12 PM by jjfcpa

    Incrementing dates

    jjfcpa

      Is there a way to increment a date by just pressing the + or - key? I've seen this in a number of software programs to speed up data entry where the dates typically only go up by one day or two.

       

      Click in the date field, then press the + or - key to increament or decrement the date?

       

      Anyone have a script to do this?

        • 1. Re: Incrementing dates
          robwoof

          I've never tried it, but you could try a Script Trigger. An "OnObjectKeystroke" trigger to something like the following may do the trick:

           

          If [ Get ( TriggerKeystroke ) = "+" or Get ( TriggerKeystroke ) = "=" ]

                 Set Field [ MyTable::DateField ; MyTable::DateField + 1 ]

          Else If [ Get ( TriggerKeystroke ) = "-" ]

                Set Field [ MyTable::DateField ; MyTable::DateField - 1 ]

          End If

           

          Note that the second condition in the If statement is to allow for the user to tap the "=/+" key without using Shift.

           

          Anyway, that is just an educated guess. Let us know how you go.

           

          Rob

          • 2. Re: Incrementing dates
            SteveMartino

            I saw that in QuickBooks, and wanted in my DB, so I made this script, dynamic to use in multiple fields and attached it to the field with an OnObjectKeystroke:

             

            Set Variable [$Field; Value:Get (ActiveFieldName)]

            Set Variable [$Table; Value:Get (ActiveFieldTableName)]

            If [Code(Get(TriggerKeystroke))=45]

              Set Field By Name [$Table & "::" & $Field; Get (ActiveFieldContents)-1]

              Exit Script [Result: False]

            Else If [Code (Get(TriggerKeystroke))=43]

              Set Field By Name [$Table & "::" & $Field; Get (ActiveFieldContents)+1]

              Exit Script [Result: False]

            End If

             

            Make sure to check "Select enntire contents on entry" in the inspector

            As I use this from my desktop keypad, it does not take into account using shift key with -, but it does allow you to just type a date into the field.

             

            I'm always open to any comments/suggestions/tweaks

            Steve

             

            On a side note, if the user hits Ctrl + instead of Shift + it opens up the Replace Field Contents dialog, so you may want to remove that with custom menus/priveledges.

            • 3. Re: Incrementing dates
              user19752

              Without table/field name

              Insert Calculated Result [Select; Get ( ActiveFieldContents ) + 1]

              • 4. Re: Incrementing dates
                BowdenData

                I have a similar script that was also modeled after Quicken/Quickbooks. Attach it to any date field with OnObjectKeystroke. Note that I have the option for the script trigger to run in both browse and find mode. I did not find that I need to have the field option of "Select entire contents on Entry" enabled. It seems to behave the same with this disabled or enabled. In my case, I am also trapping for the user pressing Shift and minus key (code 95) because the main user who wanted the function often had the shift key pressed at the same time they pressed minus. This person mainly used a laptop without an external keyboard. I am also trapping for the caps lock key being on ($tkm = 2). The final mod was to increment/decrement the date by 7 days/1 week if the Alt/Option key was held down. I seem to recall the client telling me this was also a feature of Quicken.

                 

                Some caveats with this. If the user presses Shift Option Plus keys or Shift Option Minus keys, it won't do anything. If the user enters a date manually and uses the "minus" key as a separator (instead of the forward slash or perhaps the decimal), it will put a invalid data in the field. You'll get a warning when you try to exit the field. In this solution, I have the field option as Drop Down Calendar and the option to "include icon to show/hide the calendar". This way, if the user can use the calendar to enter a date if they normally like to use the minus key when manually entering.

                 

                This rest of this client's staff pretty much all used desktop keyboards with the separate numeric keypad. They just used the plus and minus keys on there. Makes for pretty fast data entry.

                 

                If you are on a Mac, there is a very cool app from ManyTricks that allows you to see the codes associated with keys you press. I haven't looked, but bet there is something out there for Windows as well.

                 

                http://manytricks.com/keycodes/

                 

                Hope this helps someone.

                 

                Doug

                 

                 

                # Sets variable to capture trigger key

                Set Variable [$tk; Value:Code ( Get ( TriggerKeystroke ) )]

                Set Variable [$tkm; Value:Get ( TriggerModifierKeys )]

                Set Variable [$keyCodes; Value:"43¶45¶95"]

                #

                If [IsEmpty( FilterValues( $keyCodes ; $tk ) )]

                  # User typed a key other than the plus or minus

                  Exit Script [Result: True]

                End If

                #

                If [$tk = 43 and ( $tkm = 0 or $tkm = 1 or $tkm = 2 )  //plus key with no modifier key or the shift key or the caps lock key]

                  Set Variable [$plusValue; Value:1]

                Else If [$tk = 43 and ( $tkm = 8 or $tkm = 10 )  //plus key with alt/option key or the alt/option key AND the caps lock key]

                  Set Variable [$plusValue; Value:7]

                Else If [($tk = 45 or $tk = 95 ) and ( $tkm = 0 or $tkm = 1 or $tkm = 2 )  //minus key with no modifier key or the shift key or the caps lock key]

                  Set Variable [$minusValue; Value:1]

                Else If [($tk = 45 or $tk = 95 ) and ( $tkm = 8 or $tkm = 10 )   //minus key with alt/option key or the alt/option key AND the caps lock key]

                  Set Variable [$minusValue; Value:7]

                End If

                #

                If [$plusValue]

                          Set Field By Name [GetFieldName( Evaluate( Get ( ActiveFieldName ) ) ); Get ( ActiveFieldContents ) + $plusValue]

                Else If [$minusValue]

                          Set Field By Name [GetFieldName( Evaluate( Get ( ActiveFieldName ) ) ); Get ( ActiveFieldContents ) - $minusValue]

                End If

                #

                Exit Script [Result: False]

                • 5. Re: Incrementing dates
                  jjfcpa

                  You guys are the best.  Really appreciate the feedback.

                  • 6. Re: Incrementing dates
                    erolst

                    BowdenData wrote:

                     

                    […]

                    Set Field By Name [GetFieldName( Evaluate( Get ( ActiveFieldName ) ) ); …

                     

                    If no target field parameter is specified, both Set Field[] and Set Field by Name[] will automatically target the active field.

                    • 7. Re: Incrementing dates
                      BowdenData

                      Good point. I knew this about Set Field, but did not think about it for Set Field by Name. I did this many years ago, not long after Set Field by Name was added. I was probably looking for an excuse to use it and ended up with the convoluted code for whatever reason. I have edited my sample file to just use Set Field. Much cleaner.

                       

                      Set Field [Get ( ActiveFieldContents ) + $plusValue]

                      or

                      Set Field [Get ( ActiveFieldContents ) - $minusValue]

                       

                      Thanks!

                      Doug

                      • 8. Re: Incrementing dates
                        jjfcpa

                        What I have found is that in my situation where I just want to press the + or - key to increment the date field, I have to use two scripts and a global variable.

                         

                        If I try to use any of the code above in the OnObjectModify or the OnObjectKeystroke, the date value has already been replaced by the time it gets to that event.  I need to save the date field in the OnObjectEnter, then the second script will increment the date (if a + or - is entered) and put it back in the field.

                         

                        My code allows me to continually hit the + key and keep incrementing the date by 1 day.  I'm using a custom function called AlterDate() to make this work the way I want.

                         

                        BTW, can someone tell me how to copy and paste my script code from the script editor to a post?

                        • 9. Re: Incrementing dates
                          erolst

                          jjfcpa wrote:

                          BTW, can someone tell me how to copy and paste my script code from the script editor to a post?

                          On the Mac, open the script, select Print (or press Cmd-P), select PDF (button/popup) -> Open in Preview, from where you can copy the text.

                          • 10. Re: Incrementing dates
                            jjfcpa

                            Thank you!

                            • 11. Re: Incrementing dates
                              jjfcpa

                              Here is my final code that works.

                               

                              OnObjectEnter:

                               

                              Set Error Capture [ On ]

                              Set Variable [ $$Pre_Date; Value:Get ( ActiveFieldContents ) ]

                              Exit Script [ ]

                               

                               

                              OnObjectModify:  (Note - I'm using AlterDate() custom function available on Brian Dunning's custom functions site)

                               

                              Set Error Capture [ On ]

                              Set Variable [ $Field; Value:Get ( ScriptParameter ) ]
                              Set Variable [ $Character; Value:Get ( ActiveFieldContents ) ]

                              If [ $Character = "+" or $Character = "-" ]

                                Set Field By Name [ $Field; AlterDate ( $$Pre_Date ; 0 ; If( $Character = "+" ; 1 ; -1 ) ; 0 ) ]

                                Set Variable [ $$Pre_Date; Value:Get ( ActiveFieldContents ) ]

                              End if

                              Exit Script [ ]