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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use CASE Statements in Access 2

Status
Not open for further replies.

uniroyal2001

Programmer
Jun 20, 2005
6
US
Hello All,
I am trying to use this mentioned CASE statement in a Access query.
Any leads in this matter is greatly appreciated.
Code:
SELECT 
VALUE(G.TEAM,'    ') AS CBT_TEAM,
VALUE(L.PSYS_COLLECTOR_ID,'   '),
VALUE(L.PSYS_TEAM_ID,'    ') AS PSYS_TEAM,
H.LEGAL_NAME AS CUSTOMER_NAME,
CASE
    WHEN L.PSYS_COLLECTOR_ID =  'CTA'
     AND G.TEAM              <> '    '   THEN G.TEAM
    WHEN L.PSYS_COLLECTOR_ID =  'CTA'
     AND G.TEAM              =  '    '   THEN C.TEAM
    WHEN L.PSYS_COLLECTOR_ID <> '   '
     AND L.PSYS_COLLECTOR_ID <> 'CTA'    THEN L.PSYS_TEAM_ID
    WHEN G.TEAM              <> '    '   THEN G.TEAM
    WHEN C.TEAM              <> '    '   THEN C.TEAM
    ELSE                                   '????'
END AS TEAM
thanks in advance

John Pasron
 
Hi!

You can't use the Case statement in Access SQL. You can create a function in VBA that you send the fields to and you can use the Select Case function and return the appropriate value.

Also, I have never seen the Value function either. It looks like it might do the same thing as Access's Nz function but I can't be sure. Let me know what that function does also.

hth


Jeff Bridgham
Purdue University
Graduate School
Data Analyst
 
JET SQL does not have a CASE statement.
You could use IIF() which would get very complex, or you could create a VBA user function to give the result and call that in the query.

 
Any idea to just convert it in access.
The code you are seeing about is in ANSI SQL.
I tried using IIF(...)..but gave me some obscure error message.
VALUE(G.TEAM,' ') AS CBT_TEAM,
this I can convert as Nz(G.TEAM,'') AS CBT_TEAM
 
yes i tried using multiple IIFs but something in the syntax didnt work.
 
For what you are doing the SWITCH Function may work the best.
Example syntax.
switch(item="xxx",.25,item="yyy",.75,item="zzz",.55) AS rate

Switch(L.PSYS_COLLECTOR_ID = 'CTA' AND G.TEAM <> ' ',G.TEAM,L.PSYS_COLLECTOR_ID = 'CTA' AND G.TEAM = ' ',C.TEAM, etc.......


Look up in Help for default.
 
hello cmmrfrds,
so if I am to follow your steps then the final code should like this
Code:
Switch(L.PSYS_COLLECTOR_ID =  'CTA'  AND G.TEAM <> '    ' ,G.TEAM, L.PSYS_COLLECTOR_ID =  'CTA' AND G.TEAM =  '    ',C.TEAM, L.PSYS_COLLECTOR_ID <> '   ' AND L.PSYS_COLLECTOR_ID <> 'CTA', L.PSYS_TEAM_ID,G.TEAM <> '    ',G.TEAM,C.TEAM <> '    ' ,C.TEAM, '????') AS TEAM

Please confirm and thanks a mill for your help.

John P
 
Final bit is incomplete.

Switch(
L.PSYS_COLLECTOR_ID = 'CTA' AND G.TEAM <> ' ',G.TEAM,
L.PSYS_COLLECTOR_ID = 'CTA' AND G.TEAM = ' ',C.TEAM,
L.PSYS_COLLECTOR_ID <> ' ' AND L.PSYS_COLLECTOR_ID <> 'CTA', L.PSYS_TEAM_ID,
G.TEAM <> ' ',G.TEAM,
C.TEAM <> ' ',C.TEAM,
'????') AS TEAM

switch function works on pairs, so the final bit should be
true, '????') AS TEAM
or
1=1,'????') AS TEAM


Regards

Frederico Fonseca
SysSoft Integrated Ltd
 
Thanks for the update Frederico, I couldn't remember the default part and the Help is not working on my work PC.
 
Hello Frederico,
I wish I could give you 5 stars.
It worked like a charm.
Thanks a million. :)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top