Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select Case in Query 1

Status
Not open for further replies.

vr

Programmer
Oct 14, 1999
54
US
Can anyone show me an example of how to put a select case statement in an Access query?
 
I do not think you can do this 'directly'. The closest available query 'statement' would ne a series of nested "IIF"s. You can, of course, have the query call/reference a function which can include any valid VBA statement/function.




MichaelRed
mred@duvallgroup.com
There is never time to do it right but there is always time to do it over
 
The Switch() function can often be used in a query to emulate a Select Case scenario and eliminate the need for cumbersome nested If() statements.

To demonstrate, open Northwind.mdb then:

(1) Open a new query and copy/paste/run this SQL:

Code:
SELECT DISTINCT ShipCountry
FROM Orders
ORDER BY ShipCountry;

(2) You'll be greeted with a list of 21 countries. Now suppose your task was to categorize these countries by continent (NA = North America, SA = South America, EU = Europe). Delete your first query and instead copy/paste/run this SQL. (Note: I renamed ShipCountry to 'SC' just to save space-no special significance.)

Code:
 SELECT DISTINCT ShipCountry AS SC, Switch(InStr("Argentina Brazil Venezuela",[SC])>0,"SA",InStr("Canada Mexico USA",[SC])>0,"NA",True,"EU") AS Cont
FROM Orders;

(3) If all worked well, you'll have a new field showing the continent in which each country resides. Now, if either the InStr() function or the Switch() function is new to you, go to the Access help files and read up on them. Pay special attention to the examples.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top