How to turn Excel Formulas into an Access Query?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Teresa Barnacle
    New Member
    • Jan 2011
    • 10

    #1

    How to turn Excel Formulas into an Access Query?

    Can't for the life of me work these 2 formula into an Access query !

    Any help would be much appreciated ....

    L5 is [Miles]



    =IF(L5>150,130* 3.8,(IF((AND(L5 >19.9,L5<150.1) ),(L5-20)*3.8,)))

    =IF(L5>150,(L5-150)*2.4,0)
  • Jonathan Lei
    New Member
    • Jan 2011
    • 1

    #2
    Hi Teresa,

    You may try the following solutions.

    Also, you may change the names of Expr1 ,Expr2 and Table1 accordingly.

    Fields Formula:
    Expr1: IIf([Miles]>150,130*3.8,II f([Miles]>19.9 And [Miles]<150.1,([Miles]-20)*3.8))

    Expr2: IIf([Miles]>150,([Miles]-150)*2.4,0)


    Access Query:
    SELECT Table1.Miles, IIf([Miles]>150,130*3.8,II f([Miles]>19.9 And [Miles]<150.1,([Miles]-20)*3.8)) AS Expr1, IIf([Miles]>150,([Miles]-150)*2.4,0) AS Expr2
    FROM Table1;


    Jonathan
    Last edited by Jonathan Lei; Jan 17 '11, 11:31 PM. Reason: Adding more details.

    Comment

    • ADezii
      Recognized Expert Expert
      • Apr 2006
      • 8834

      #3
      1. Sample SQL Statement:
        Code:
        SELECT A.Miles, fCalcWhatever([Miles]) AS Expr
        FROM A
        WHERE A.Miles Is Not Null;
      2. Function Definition:
        Code:
        Public Function fCalcWhatever(sngMiles As Single) As Variant
        If sngMiles >= 150 Then
          fCalcWhatever = (130 * 3.8)
        ElseIf sngMiles > 19.9 And sngMiles < 150 Then
          fCalcWhatever = ((sngMiles - 20) * 3.8)
        Else        'sngMiles <= 19.9
          fCalcWhatever = sngMiles      'Not sure
        End If
        End Function
      3. Sample Date:
        Code:
        Miles
        20.1
        149
        174
        17
        14.3
      4. Results based on Sample Data:
        Code:
        Miles	Expr
        20.1	   0.380001449584961
        149	  490.2
        174	  494
        17	    17
        14.3	  14.3

      Comment

      • Teresa Barnacle
        New Member
        • Jan 2011
        • 10

        #4
        IT WORKS !!!

        THANK YOU SOOOOOOO much to all who helped.

        Regards

        One very happy person

        Comment

        • Teresa Barnacle
          New Member
          • Jan 2011
          • 10

          #5
          Need help again,sorry

          B: IIf([Miles]>150,130*3.8,II f([Miles]>19.9 And [Miles]<150.1,([Miles]-20)*3.8))

          How do I put in this "if null put me a 0 in" ?

          My other sums don't work if I don't put a zero in, sometimes I could scream !

          Regards, Teresa

          Comment

          • ADezii
            Recognized Expert Expert
            • Apr 2006
            • 8834

            #6
            Try:
            Code:
            IIf(IsNull([Miles]),0,IIf([Miles]>150,130*3.8,IIf([Miles]>19.9 And [Miles]<150.1,([Miles]-20)*3.8)))

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32669

              #7
              Teresa, your formula doesn't specify any value for when [Miles] not greater than 19.9.

              If we assume that you are intending to say 20 and above, and that less than 20 returns a value of zero too, then try :

              Code:
              IIf(IsNull([Miles]),0,IIf([Miles]>150,130,IIf([Miles]<20,0,[Miles]-20))) * 3.8

              Comment

              • Teresa Barnacle
                New Member
                • Jan 2011
                • 10

                #8
                Done and works perfect, you're all brilliant !

                Thanks, Teresa

                Comment

                • NeoPa
                  Recognized Expert Moderator MVP
                  • Oct 2006
                  • 32669

                  #9
                  A pleasure Teresa.

                  To be fair, I think ADezii's version would have worked well too.

                  Comment

                  Working...