IF commands in a form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • j0rdanf1
    New Member
    • Oct 2006
    • 35

    IF commands in a form

    Hello,

    Total newby when it comes to access so im hoping someone can put me in the right direction,

    I have a form which needs to be able to adjust the warrenty finish date by either 3 or 4 years from the warrenty start date depending if it has the word laptop (3 years) or desktop(4 years) in it.

    This is what I have, its the best I could come up with but alas it doesnt work. Any ideas?

    = IF [Model Type].AfterUpdate Like ("LAPTOP") THEN ("yyyy",3,[Warrenty Start] ) ELSE ("yyyy",4,[Warrenty Start])
  • PEB
    Recognized Expert Top Contributor
    • Aug 2006
    • 1418

    #2
    The structure that you want to use is:

    IIF(Condition, true value, False value)

    = IIF ([Model Type]="LAPTOP" ,datepart("yyyy ",Warrenty Start] ) ,Datepart("yyyy ",[Warrenty Start]))

    :)

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32634

      #3
      The following code should work for you.
      It's basically what PEB said but with a few minor amendments.

      Code:
      =DateAdd("yyyy", IIf(UCase([Model Type])="LAPTOP", 4, 5), [Warranty Start])
      NB. I respelt Warranty - this may not match what's in your db.
      I used UCase() in case values in [Model Type] are entered by humans (dodgy lot they are).
      I strongly recommend not to use names with embedded spaces. Your choice, but they are likely to cause you more problems in the long run.

      Comment

      • j0rdanf1
        New Member
        • Oct 2006
        • 35

        #4
        Originally posted by NeoPa
        The following code should work for you.
        It's basically what PEB said but with a few minor amendments.

        Code:
        =DateAdd("yyyy", IIf(UCase([Model Type])="LAPTOP", 4, 5), [Warranty Start])
        NB. I respelt Warranty - this may not match what's in your db.
        I used UCase() in case values in [Model Type] are entered by humans (dodgy lot they are).
        I strongly recommend not to use names with embedded spaces. Your choice, but they are likely to cause you more problems in the long run.
        ----

        tried both and no luck

        the word laptop in the search is only part of the text e.g IBM x31 laptop, would that make a difference

        Comment

        • pks00
          Recognized Expert Contributor
          • Oct 2006
          • 280

          #5
          In that case, just tweak the IIF statement provided earlier by Peb and NeoPa, whichever one u used

          e.g.

          [Model Type] LIKE "*LAPTOP*"

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32634

            #6
            Not forgetting the UCase() and the spelling of the field name.
            I spelt it to match the word rather than your posted field name so if you tried the code by simply copying and pasting, then it certainly wouldn't have worked.
            pks00's is the correct way to formulate the <condition> within the IIf() function though.

            Comment

            • j0rdanf1
              New Member
              • Oct 2006
              • 35

              #7
              Originally posted by NeoPa
              Not forgetting the UCase() and the spelling of the field name.
              I spelt it to match the word rather than your posted field name so if you tried the code by simply copying and pasting, then it certainly wouldn't have worked.
              pks00's is the correct way to formulate the <condition> within the IIf() function though.
              Yes i have done that but i still dont think i understand the code,

              ([Model Type] LIKE "*LAPTOP*" ,datepart("yyyy ",3,[Warrenty Start] ) ,Datepart("yyyy ",4,[Warrenty Start]))

              this is what i have inputted

              cheers

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32634

                #8
                This is what you need if you want to keep your current fieldnames.
                Code:
                =DateAdd("yyyy", IIf(UCase([Model Type]) Like "*LAPTOP*", 4, 5), [Warrenty Start])

                Comment

                • j0rdanf1
                  New Member
                  • Oct 2006
                  • 35

                  #9
                  Cheers,

                  Thanks for all your input, should work a treat

                  Comment

                  Working...