+ Reply to Thread
Results 1 to 5 of 5

SWITCH statement

  1. #1
    Forum Contributor
    Join Date
    05-08-2013
    Location
    California
    MS-Off Ver
    Excel 2013
    Posts
    102

    SWITCH statement



    I have the following table named [SalesByCity]:

    City;SalesAmount
    ---------------------------
    Los Angeles;1000
    New York;1200
    Boston;700

    If I were using T-SQL, I could use the CASE statement, but I need to do this in Access (which does not support the CASE statement). However, Access SQL does support the SWITCH statement. Is there a way to use a SWITCH statement in Access to get a query that assigns 1 million to all cities except for Boston (in which case the dollar amount is 500000)? I am also trying to avoid using the IIF statement.

    The result would be something like…

    City;Valuation
    ---------------------------
    Los Angeles;1000000
    New York;1000000
    Boston;500000

    I thought that my query might go something like this, but I get an error message:

    Please Login or Register  to view this content.
    Thanks.
    Last edited by Dimitrov; 05-23-2014 at 02:56 PM.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    Get rid of [salesamount]=, having that will mean the SWITCH will only return True/False.

    By the way, why not use Iif?

    That would probably be simpler.
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor
    Join Date
    05-08-2013
    Location
    California
    MS-Off Ver
    Excel 2013
    Posts
    102

    Re: SWITCH statement

    Hi, Norie.

    I wanted to see if the SWITCH statement would have any performance improvements over the IIF statement, but, alas could not get the right syntax for the SWITCH statement to work.

    Later on, I would also like to do things such as tripling the sales in all cities except Boston (in which case the sales would be doubled). Thus I tried this, but it failed me...

    Please Login or Register  to view this content.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: SWITCH statement

    SWITCH returns a value, it won't carry out an operation on a field.

    To use SWITCH in this example return the multiplier.
    Formula: copy to clipboard
    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    05-08-2013
    Location
    California
    MS-Off Ver
    Excel 2013
    Posts
    102

    Re: SWITCH statement

    Hi, Norie.

    That is a very good idea you have there. Thank you so much!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 0
    Last Post: 04-22-2014, 10:59 AM
  2. VBA switch statement with a twist?
    By pwillia in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-12-2012, 01:41 PM
  3. Switch statement
    By gtmeloney in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-06-2009, 01:45 PM
  4. Switch(case) statement(enhanced if statement)
    By zapopaul in forum Excel General
    Replies: 6
    Last Post: 10-24-2008, 06:53 PM
  5. switch statement
    By tkaplan in forum Excel General
    Replies: 1
    Last Post: 07-13-2005, 04:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1