using CASE in access

  • Thread starter Thread starter alecgreen
  • Start date Start date
A

alecgreen

Hi

I am trying to understand the use of CASE statement in access, can
anyone please give me an easy to understand example, with how to call
it within a query?

Many Thanks

Alec
 
Case statement is not supported in Access SQL.

Case can be used in VBA procedures.

In queries use can use nested IIF statements.

Or you can use the VBA functions Switch and Choose.

Or you can build a custom VBA function that returns a value based on the use
of the VBA Case structure.

John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County
 
Case statement is not supported in Access SQL.

Case can be used in VBA procedures.

In queries use can use nested IIF statements.

Or you can use the VBA functions Switch and Choose.

Or you can build a custom VBA function that returns a value based on the use
of the VBA Case structure.

John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County







- Show quoted text -

Hi, Thanks for the response - how about an example using VBA then
please

Alec
 
Here is a simple VBA function

Public Function fReturnAValue(vIn)

SELECT Case vIn
Case "A"
fReturnAValue= "Abalone"
Case "S"
fReturnAValue= "Swordfish"
Case "T"
fReturnAValue= "Tuna"
Case Else
fReturnAValue= vIn
END SELECT

End Function

In a query you could call that using
SELECT fReturnAValue([SomeTable].[SomeField]) as FishType
FROM ...

John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County
 
Here is a simple VBA function

Public Function fReturnAValue(vIn)

   SELECT Case vIn
      Case "A"
         fReturnAValue= "Abalone"
      Case "S"
         fReturnAValue= "Swordfish"
      Case "T"
         fReturnAValue= "Tuna"
      Case Else
         fReturnAValue= vIn
   END SELECT

End Function

In a query you could call that using
SELECT fReturnAValue([SomeTable].[SomeField]) as FishType
FROM ...

John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County


Hi, Thanks for the response - how about an example using VBA then
please
Alec- Hide quoted text -

- Show quoted text -

Brilliant! Thank you Guys
 
Here is a simple VBA function

Public Function fReturnAValue(vIn)

SELECT Case vIn
Case "A"
fReturnAValue= "Abalone"
Case "S"
fReturnAValue= "Swordfish"
Case "T"
fReturnAValue= "Tuna"
Case Else
fReturnAValue= vIn
END SELECT

End Function

In a query you could call that using
SELECT fReturnAValue([SomeTable].[SomeField]) as FishType
FROM ...

But that's storing data in code -- that's a lookup table.
 
Back
Top