Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 19th, 2005, 02:18 PM
shank
Guest
 
Posts: n/a
Default sorting columns with Access

Is the CASE statement allowed when querying an Access database?
Assuming so, what is the correct syntax?
thanks

SELECT OrderNo, Description, Type, Label
FROM Stock
WHERE Manuf = 'M'
ORDER BY
CASE 'C' WHEN 'orderno_asc' THEN OrderNo END ASC,
CASE 'C' WHEN 'description_asc' THEN Description END ASC,
CASE 'C' WHEN 'type_asc' THEN Type END ASC,
CASE 'C' WHEN 'label_asc' THEN Label END ASC,
CASE 'C' WHEN 'orderno_asc' THEN OrderNo END DESC,
CASE 'C' WHEN 'description_asc' THEN Description END DESC,
CASE 'C' WHEN 'type_asc' THEN Type END DESC,
CASE 'C' WHEN 'label_asc' THEN Label END DESC


  #2  
Old July 19th, 2005, 02:18 PM
Bob Barrows [MVP]
Guest
 
Posts: n/a
Default Re: sorting columns with Access

shank wrote:[color=blue]
> Is the CASE statement allowed when querying an Access database?[/color]

You could have found out much quicker by using Access online help, but no,
CASE is not supported by JetSQL.
[color=blue]
> Assuming so, what is the correct syntax?
> thanks
>
> SELECT OrderNo, Description, Type, Label
> FROM Stock
> WHERE Manuf = 'M'
> ORDER BY
> CASE 'C' WHEN 'orderno_asc' THEN OrderNo END ASC,[/color]

?? What is 'C'? As written, it's a literal string that will never be equal
to 'orderno_asc'.


You have to use the VBA Iif() function, also known as "Immediate If".
Assuming 'C' is actually intended to represent a parameter, here is how it
would look:

Iif([C] = 'orderno_asc',OrderNo,0) ASC,
....

Bob Barrows
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.


  #3  
Old July 19th, 2005, 02:19 PM
Chris Hohmann
Guest
 
Posts: n/a
Default Re: sorting columns with Access

"shank" <shank@tampabay.rr.com> wrote in message
news:ur0$cimbEHA.712@TK2MSFTNGP11.phx.gbl...[color=blue]
> Is the CASE statement allowed when querying an Access database?
> Assuming so, what is the correct syntax?
> thanks
>
> SELECT OrderNo, Description, Type, Label
> FROM Stock
> WHERE Manuf = 'M'
> ORDER BY
> CASE 'C' WHEN 'orderno_asc' THEN OrderNo END ASC,
> CASE 'C' WHEN 'description_asc' THEN Description END ASC,
> CASE 'C' WHEN 'type_asc' THEN Type END ASC,
> CASE 'C' WHEN 'label_asc' THEN Label END ASC,
> CASE 'C' WHEN 'orderno_asc' THEN OrderNo END DESC,
> CASE 'C' WHEN 'description_asc' THEN Description END DESC,
> CASE 'C' WHEN 'type_asc' THEN Type END DESC,
> CASE 'C' WHEN 'label_asc' THEN Label END DESC
>[/color]

In addition to the IIF function Bob mentioned, you may also want to
investigate the SWITCH function. The nested IIF functions could get pretty
hairy in this particular situation. Bob, haven't we had this conversation
before? :)

http://groups.google.com/groups?selm...TNGP09.phx.gbl

-Chris Hohmann


  #4  
Old July 19th, 2005, 02:19 PM
Bob Barrows [MVP]
Guest
 
Posts: n/a
Default Re: sorting columns with Access

Chris Hohmann wrote:[color=blue]
> "shank" <shank@tampabay.rr.com> wrote in message
> news:ur0$cimbEHA.712@TK2MSFTNGP11.phx.gbl...[color=green]
>> Is the CASE statement allowed when querying an Access database?
>> Assuming so, what is the correct syntax?
>> thanks
>>
>> SELECT OrderNo, Description, Type, Label
>> FROM Stock
>> WHERE Manuf = 'M'
>> ORDER BY
>> CASE 'C' WHEN 'orderno_asc' THEN OrderNo END ASC,
>> CASE 'C' WHEN 'description_asc' THEN Description END ASC,
>> CASE 'C' WHEN 'type_asc' THEN Type END ASC,
>> CASE 'C' WHEN 'label_asc' THEN Label END ASC,
>> CASE 'C' WHEN 'orderno_asc' THEN OrderNo END DESC,
>> CASE 'C' WHEN 'description_asc' THEN Description END DESC,
>> CASE 'C' WHEN 'type_asc' THEN Type END DESC,
>> CASE 'C' WHEN 'label_asc' THEN Label END DESC
>>[/color]
>
> In addition to the IIF function Bob mentioned, you may also want to
> investigate the SWITCH function. The nested IIF functions could get
> pretty hairy in this particular situation. Bob, haven't we had this
> conversation before? :)
>
>[/color]
http://groups.google.com/groups?selm...TNGP09.phx.gbl[color=blue]
>[/color]
Guilty. I forgot about Switch again. However, in this case I do not think it
is appropriate unless all those columns have the same datatype.

Bob Barrows

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.


  #5  
Old July 19th, 2005, 02:19 PM
Chris Hohmann
Guest
 
Posts: n/a
Default Re: sorting columns with Access

"Bob Barrows [MVP]" <reb01501@NOyahoo.SPAMcom> wrote in message
news:OeG3YUybEHA.2840@TK2MSFTNGP11.phx.gbl...[color=blue]
> Chris Hohmann wrote:[color=green]
> > "shank" <shank@tampabay.rr.com> wrote in message
> > news:ur0$cimbEHA.712@TK2MSFTNGP11.phx.gbl...[color=darkred]
> >> Is the CASE statement allowed when querying an Access database?
> >> Assuming so, what is the correct syntax?
> >> thanks
> >>
> >> SELECT OrderNo, Description, Type, Label
> >> FROM Stock
> >> WHERE Manuf = 'M'
> >> ORDER BY
> >> CASE 'C' WHEN 'orderno_asc' THEN OrderNo END ASC,
> >> CASE 'C' WHEN 'description_asc' THEN Description END ASC,
> >> CASE 'C' WHEN 'type_asc' THEN Type END ASC,
> >> CASE 'C' WHEN 'label_asc' THEN Label END ASC,
> >> CASE 'C' WHEN 'orderno_asc' THEN OrderNo END DESC,
> >> CASE 'C' WHEN 'description_asc' THEN Description END DESC,
> >> CASE 'C' WHEN 'type_asc' THEN Type END DESC,
> >> CASE 'C' WHEN 'label_asc' THEN Label END DESC
> >>[/color]
> >
> > In addition to the IIF function Bob mentioned, you may also want to
> > investigate the SWITCH function. The nested IIF functions could get
> > pretty hairy in this particular situation. Bob, haven't we had this
> > conversation before? :)
> >
> >[/color]
>[/color]
http://groups.google.com/groups?selm...TNGP09.phx.gbl[color=blue][color=green]
> >[/color]
> Guilty. I forgot about Switch again. However, in this case I do not think[/color]
it[color=blue]
> is appropriate unless all those columns have the same datatype.[/color]

The return value of the SWITCH function is variant so I think we're ok.


 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles