MS Access Update Query with an IIF and DATEADD functions











up vote
1
down vote

favorite












I have 6 fields in my "table1".




  • Current Date

  • Next Adjustment Date

  • Months Elapsed

  • Index 0

  • Index 1

  • Index 2.


I'm trying to create an update query for Index 1 where if the (Current Date+Months Elapsed) < Next Adjustment Date then Index 1 is Index 0 if not I want Index 1 to stay at whatever value it currently has.



I tried this



iif(DateAdd("m",[Table1]![Months Elapsed],[Table1]![Current Date])<[Table1]![Next Adjustment Date], [Table1]![Index 0]


As soon as I put in the [Table1]![Next Adjustment Date] in the iif formula I receive "The expression you entered contains invalid syntax" error.










share|improve this question




























    up vote
    1
    down vote

    favorite












    I have 6 fields in my "table1".




    • Current Date

    • Next Adjustment Date

    • Months Elapsed

    • Index 0

    • Index 1

    • Index 2.


    I'm trying to create an update query for Index 1 where if the (Current Date+Months Elapsed) < Next Adjustment Date then Index 1 is Index 0 if not I want Index 1 to stay at whatever value it currently has.



    I tried this



    iif(DateAdd("m",[Table1]![Months Elapsed],[Table1]![Current Date])<[Table1]![Next Adjustment Date], [Table1]![Index 0]


    As soon as I put in the [Table1]![Next Adjustment Date] in the iif formula I receive "The expression you entered contains invalid syntax" error.










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have 6 fields in my "table1".




      • Current Date

      • Next Adjustment Date

      • Months Elapsed

      • Index 0

      • Index 1

      • Index 2.


      I'm trying to create an update query for Index 1 where if the (Current Date+Months Elapsed) < Next Adjustment Date then Index 1 is Index 0 if not I want Index 1 to stay at whatever value it currently has.



      I tried this



      iif(DateAdd("m",[Table1]![Months Elapsed],[Table1]![Current Date])<[Table1]![Next Adjustment Date], [Table1]![Index 0]


      As soon as I put in the [Table1]![Next Adjustment Date] in the iif formula I receive "The expression you entered contains invalid syntax" error.










      share|improve this question















      I have 6 fields in my "table1".




      • Current Date

      • Next Adjustment Date

      • Months Elapsed

      • Index 0

      • Index 1

      • Index 2.


      I'm trying to create an update query for Index 1 where if the (Current Date+Months Elapsed) < Next Adjustment Date then Index 1 is Index 0 if not I want Index 1 to stay at whatever value it currently has.



      I tried this



      iif(DateAdd("m",[Table1]![Months Elapsed],[Table1]![Current Date])<[Table1]![Next Adjustment Date], [Table1]![Index 0]


      As soon as I put in the [Table1]![Next Adjustment Date] in the iif formula I receive "The expression you entered contains invalid syntax" error.







      ms-access dateadd iif-function






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 8 at 17:37









      HansUp

      86k1156102




      86k1156102










      asked Nov 7 at 21:26









      Ian

      615




      615
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote













          iif( is not matched by a closing )



          Work out the syntax in a SELECT query. Then use that working field expression in your UPDATE. Here is my suggestion ...



          SELECT
          IIf
          (
          DateAdd('m', t1.[Months Elapsed], t1.[Current Date]) < t1.[Next Adjustment Date],
          t1.[Index 0]
          )
          FROM Table1 AS t1;


          Note your IIf lacked the third argument, which is the value to return when the condition (the first argument) is not True. The database engine will not complain and will return Null in that situation. See also this answer.



          However your description suggests you may actually want this ...



              IIf
          (
          DateAdd('m', t1.[Months Elapsed], t1.[Current Date]) < t1.[Next Adjustment Date],
          t1.[Index 0],
          t1.[Index 1]
          )


          ... but for an UPDATE, it makes more sense to me to move that condition to the WHERE clause and update only those rows ...



          UPDATE Table1 AS t1
          SET t1.[Index 1] = t1.[Index 0]
          WHERE DateAdd('m', t1.[Months Elapsed], t1.[Current Date]) < t1.[Next Adjustment Date];





          share|improve this answer






























            up vote
            0
            down vote













            You don't have the false part of the IIf specified. Try this;



            IIf(DateAdd("m",[Table1]![Months Elapsed],[Table1]![Current Date])< [Table1]![Next Adjustment Date], [Table1]![Index 0], [Table1]![Index 1])


            However I'm concerned that you have apparently fields stored for Current Date and Months Elapsed. Aren't these both calculated on the fly? They should be.






            share|improve this answer





















              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














               

              draft saved


              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53198072%2fms-access-update-query-with-an-iif-and-dateadd-functions%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              1
              down vote













              iif( is not matched by a closing )



              Work out the syntax in a SELECT query. Then use that working field expression in your UPDATE. Here is my suggestion ...



              SELECT
              IIf
              (
              DateAdd('m', t1.[Months Elapsed], t1.[Current Date]) < t1.[Next Adjustment Date],
              t1.[Index 0]
              )
              FROM Table1 AS t1;


              Note your IIf lacked the third argument, which is the value to return when the condition (the first argument) is not True. The database engine will not complain and will return Null in that situation. See also this answer.



              However your description suggests you may actually want this ...



                  IIf
              (
              DateAdd('m', t1.[Months Elapsed], t1.[Current Date]) < t1.[Next Adjustment Date],
              t1.[Index 0],
              t1.[Index 1]
              )


              ... but for an UPDATE, it makes more sense to me to move that condition to the WHERE clause and update only those rows ...



              UPDATE Table1 AS t1
              SET t1.[Index 1] = t1.[Index 0]
              WHERE DateAdd('m', t1.[Months Elapsed], t1.[Current Date]) < t1.[Next Adjustment Date];





              share|improve this answer



























                up vote
                1
                down vote













                iif( is not matched by a closing )



                Work out the syntax in a SELECT query. Then use that working field expression in your UPDATE. Here is my suggestion ...



                SELECT
                IIf
                (
                DateAdd('m', t1.[Months Elapsed], t1.[Current Date]) < t1.[Next Adjustment Date],
                t1.[Index 0]
                )
                FROM Table1 AS t1;


                Note your IIf lacked the third argument, which is the value to return when the condition (the first argument) is not True. The database engine will not complain and will return Null in that situation. See also this answer.



                However your description suggests you may actually want this ...



                    IIf
                (
                DateAdd('m', t1.[Months Elapsed], t1.[Current Date]) < t1.[Next Adjustment Date],
                t1.[Index 0],
                t1.[Index 1]
                )


                ... but for an UPDATE, it makes more sense to me to move that condition to the WHERE clause and update only those rows ...



                UPDATE Table1 AS t1
                SET t1.[Index 1] = t1.[Index 0]
                WHERE DateAdd('m', t1.[Months Elapsed], t1.[Current Date]) < t1.[Next Adjustment Date];





                share|improve this answer

























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  iif( is not matched by a closing )



                  Work out the syntax in a SELECT query. Then use that working field expression in your UPDATE. Here is my suggestion ...



                  SELECT
                  IIf
                  (
                  DateAdd('m', t1.[Months Elapsed], t1.[Current Date]) < t1.[Next Adjustment Date],
                  t1.[Index 0]
                  )
                  FROM Table1 AS t1;


                  Note your IIf lacked the third argument, which is the value to return when the condition (the first argument) is not True. The database engine will not complain and will return Null in that situation. See also this answer.



                  However your description suggests you may actually want this ...



                      IIf
                  (
                  DateAdd('m', t1.[Months Elapsed], t1.[Current Date]) < t1.[Next Adjustment Date],
                  t1.[Index 0],
                  t1.[Index 1]
                  )


                  ... but for an UPDATE, it makes more sense to me to move that condition to the WHERE clause and update only those rows ...



                  UPDATE Table1 AS t1
                  SET t1.[Index 1] = t1.[Index 0]
                  WHERE DateAdd('m', t1.[Months Elapsed], t1.[Current Date]) < t1.[Next Adjustment Date];





                  share|improve this answer














                  iif( is not matched by a closing )



                  Work out the syntax in a SELECT query. Then use that working field expression in your UPDATE. Here is my suggestion ...



                  SELECT
                  IIf
                  (
                  DateAdd('m', t1.[Months Elapsed], t1.[Current Date]) < t1.[Next Adjustment Date],
                  t1.[Index 0]
                  )
                  FROM Table1 AS t1;


                  Note your IIf lacked the third argument, which is the value to return when the condition (the first argument) is not True. The database engine will not complain and will return Null in that situation. See also this answer.



                  However your description suggests you may actually want this ...



                      IIf
                  (
                  DateAdd('m', t1.[Months Elapsed], t1.[Current Date]) < t1.[Next Adjustment Date],
                  t1.[Index 0],
                  t1.[Index 1]
                  )


                  ... but for an UPDATE, it makes more sense to me to move that condition to the WHERE clause and update only those rows ...



                  UPDATE Table1 AS t1
                  SET t1.[Index 1] = t1.[Index 0]
                  WHERE DateAdd('m', t1.[Months Elapsed], t1.[Current Date]) < t1.[Next Adjustment Date];






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 8 at 17:47

























                  answered Nov 8 at 17:30









                  HansUp

                  86k1156102




                  86k1156102
























                      up vote
                      0
                      down vote













                      You don't have the false part of the IIf specified. Try this;



                      IIf(DateAdd("m",[Table1]![Months Elapsed],[Table1]![Current Date])< [Table1]![Next Adjustment Date], [Table1]![Index 0], [Table1]![Index 1])


                      However I'm concerned that you have apparently fields stored for Current Date and Months Elapsed. Aren't these both calculated on the fly? They should be.






                      share|improve this answer

























                        up vote
                        0
                        down vote













                        You don't have the false part of the IIf specified. Try this;



                        IIf(DateAdd("m",[Table1]![Months Elapsed],[Table1]![Current Date])< [Table1]![Next Adjustment Date], [Table1]![Index 0], [Table1]![Index 1])


                        However I'm concerned that you have apparently fields stored for Current Date and Months Elapsed. Aren't these both calculated on the fly? They should be.






                        share|improve this answer























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          You don't have the false part of the IIf specified. Try this;



                          IIf(DateAdd("m",[Table1]![Months Elapsed],[Table1]![Current Date])< [Table1]![Next Adjustment Date], [Table1]![Index 0], [Table1]![Index 1])


                          However I'm concerned that you have apparently fields stored for Current Date and Months Elapsed. Aren't these both calculated on the fly? They should be.






                          share|improve this answer












                          You don't have the false part of the IIf specified. Try this;



                          IIf(DateAdd("m",[Table1]![Months Elapsed],[Table1]![Current Date])< [Table1]![Next Adjustment Date], [Table1]![Index 0], [Table1]![Index 1])


                          However I'm concerned that you have apparently fields stored for Current Date and Months Elapsed. Aren't these both calculated on the fly? They should be.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 8 at 11:04









                          Minty

                          1,4821411




                          1,4821411






























                               

                              draft saved


                              draft discarded



















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53198072%2fms-access-update-query-with-an-iif-and-dateadd-functions%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Schultheiß

                              Liste der Kulturdenkmale in Wilsdruff

                              Android Play Services Check