8

I have trying to write a query in Access 2010. I have a table:

power

Table name is power. I have trying to write IF statement:

Select IIf(power.gain_type = 'D', power.gain_max + 2.15)

If gain_type equals D, then gain_max sum 2.15

For example:

14.8 + 2.15 = 16.95.

Thanks in advance!

Vivek Jain
  • 3,811
  • 6
  • 30
  • 47
Juronis
  • 495
  • 4
  • 8
  • 18

3 Answers3

10

Now I wondering how to insert an ELSEIF statment. "IF (gain_type='D'){gain_max+2.15} ELSEIF (gain_type='I'){gain_max-2.15} ELSE {gain_max}

You can either use SWITCH

Select power.gain_max + Switch(power.gain_type='D', 2.15,
                               power.gain_type='I', -2.15,
                               true, 0)
from power

or nest/chain the IIFs

Select power.gain_max + IIf(power.gain_type='D', 2.15,
                        IIf(power.gain_type='I', -2.15, 0))
from power

Original

This does the select

Select IIf(power.gain_type='D', power.gain_max+2.15, power.gain_max)
from power

Are you trying to update?

update power
set gain_max = gain_max+2.15
where gain_type='D'

You can also use the fact that TRUE = -1 in Access

Select power.gain_max-2.15*(power.gain_type='D')
from power

References

RichardTheKiwi
  • 105,798
  • 26
  • 196
  • 262
  • Now I wondering how to insert an ELSEIF statment. "IF (gain_type='D'){gain_max+2.15} ELSEIF (gain_type='I'){gain_max-2.15} ELSE {gain_max}. – Juronis Mar 18 '11 at 12:13
1

The syntax is iif(condition, value_if_true, value_if_false). If you add a third parameter you should be fine:

IIf(power.gain_type='D', 
    power.gain_max+2.15,
    power.gain_max)
Andomar
  • 232,371
  • 49
  • 380
  • 404
0

Result: IIf([gain_type]="D",[gain_max]+2.15,[gain_max])

enter image description here

SausageFingers
  • 1,796
  • 5
  • 31
  • 52