Switch() with criteria of Like wildcard

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kpfunf
    New Member
    • Feb 2008
    • 78

    Switch() with criteria of Like wildcard

    I have a field whose criteria I can't quite work out.

    I have an Option Group on a form. If the value is 2, 3 or 4, then criteria equals as shown below (works fine). The kicker is value 1: I want to return all records (Like "*"). However, using the Switch statement, I can't seem to pass that through. I've tried all sorts of combinations of single and double quotes around the Like "*", but to no avail. I know someone out there knows the quick fix! Thanks.


    [code=sql]Switch([Forms]![Accounts]![PaymentTypeOpti onGroupFrame]=1,"Like '*'",[Forms]![Accounts]![PaymentTypeOpti onGroupFrame]=2,"Current Charges",[Forms]![Accounts]![PaymentTypeOpti onGroupFrame]=3,"Fuel Card",[Forms]![Accounts]![PaymentTypeOpti onGroupFrame]=4,"Prepay"[/code]
  • nico5038
    Recognized Expert Specialist
    • Nov 2006
    • 3080

    #2
    Originally posted by kpfunf
    I have a field whose criteria I can't quite work out.

    I have an Option Group on a form. If the value is 2, 3 or 4, then criteria equals as shown below (works fine). The kicker is value 1: I want to return all records (Like "*"). However, using the Switch statement, I can't seem to pass that through. I've tried all sorts of combinations of single and double quotes around the Like "*", but to no avail. I know someone out there knows the quick fix! Thanks.


    [code=sql]Switch([Forms]![Accounts]![PaymentTypeOpti onGroupFrame]=1,"Like '*'",[Forms]![Accounts]![PaymentTypeOpti onGroupFrame]=2,"Current Charges",[Forms]![Accounts]![PaymentTypeOpti onGroupFrame]=3,"Fuel Card",[Forms]![Accounts]![PaymentTypeOpti onGroupFrame]=4,"Prepay"[/code]
    You'll need to make the comparison always a LIKE like:
    [code=sql]
    where x like Switch(...)
    [/code]

    Now use for the 1 just the value "*"

    Nic;o)

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32656

      #3
      Your Switch() function is trying to return SQL code, rather than simply returning a value. It doesn't work like that I'm afraid, and that's why item 1 will never work for you.

      That sort of technique can be used if you're building up a SQL string from somewhere else (VBA for instance) which is itself (later) passed on to the SQL engine to process. It can't can't work within the SQL process itself though.

      Nico's suggestion is a very usable alternative.

      Comment

      • kpfunf
        New Member
        • Feb 2008
        • 78

        #4
        Works great, thank you very much.

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32656

          #5
          You're welcome :)

          Comment

          Working...