Syntax Error in Access Query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Lori Aumick
    New Member
    • Feb 2012
    • 1

    Syntax Error in Access Query

    I have the following in my query and it's giving an error - Syntax Error.
    Code:
    Available Savings: IIf([Sub to Plt Date]>1/1/2012,[Current _ann trans cost]-[Proposed_ann trans cost]/12*(13-(DatePart("m",[Sub to Plt Date])),[Current _ann trans cost]-[Proposed_ann trans cost]))
    Would somebody be able to explain why this might be.
    Last edited by NeoPa; Feb 3 '12, 12:41 AM. Reason: Converted to decent question
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    When using a literal date, you need to enclose it in hash (#) symbols.

    Comment

    • ADezii
      Recognized Expert Expert
      • Apr 2006
      • 8834

      #3
      Not 100% sure on this:
      Code:
      IIf([Sub to Plt Date] > 1/1 /2012, [Current _ann trans cost] - [Proposed_ann trans cost] / 12 * (13 - (DatePart("m", [Sub to Plt Date]))), [Current _ann trans cost] - [Proposed_ann trans cost])

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        @Rabbit:
        Does the additional ')' at the end of IIf() belong there?

        Comment

        • Rabbit
          Recognized Expert MVP
          • Jan 2007
          • 12517

          #5
          @ADezii, I hadn't checked but yeah, their parentheses are off.

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32633

            #6
            Unless I mucked up with the editing (and I didn't - I've checked back in the log) then the parentheses balance out correctly.

            The date is incorrect however. See Literal DateTimes and Their Delimiters (#) for all the details on how that needs to be handled (Adding hashes (#) will be OK for databases run in North America but elsewhere it's more complicated than that).

            Comment

            • ADezii
              Recognized Expert Expert
              • Apr 2006
              • 8834

              #7
              the parentheses balance out correctly
              They do, but wouldn't an additional ')' at the end indicate a Grouping on the True and False Clauses, since there is none on the False Clause? This can never happen, can it?
              Code:
              IIf([Sub to Plt Date] > #1/1/2012#, [Current _ann trans cost] - [Proposed_ann trans cost] / 12 * (13 - (DatePart("m", [Sub to Plt Date]))), [Current _ann trans cost] - [Proposed_ann trans cost])

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32633

                #8
                Of course they do ADezii. What am I thinking?

                I looked at it and, even though clearly the parentheses are all balanced, I was sure that the closing parenthesis near the end was the correct place (when clearly it isn't as you quite correctly highlighted). I don't know how I managed to get it so wrong. Not just to miss it, but also to consider it correct and yours wrong.

                Comment

                Working...