IIF function

D

Don

Hello - I created this IIF function to evaluate a field
called Record_ID. I want to assign each record to a group
if it falls into a certain range. When I run the query
that contains this IIF function all my values are
Assigned to "Group A" Is there something wrong with my
syntax?


IIf([Record_ID]>0<=2000,"Group A",IIf([Record_ID]
=2001<=3000,"Group B",IIf([Record_ID]>=3001<=4000,"Group
C",IIf([Record_ID]>=4001<=5000,"Group D","NA"))))


Thanks
Don
 
T

Tonín

Easier than IIf, let you use "switch"


Switch([RecordId]<=2000,"Group A",[RecordId]<=3000,"Group
B",[RecordId]<=4000,"Group C",[RecordId]<=5000,"Group
D",[RecordId]>5000,"NA")



Tonín

Don said:
Hello - I created this IIF function to evaluate a field
called Record_ID. I want to assign each record to a group
if it falls into a certain range. When I run the query
that contains this IIF function all my values are
Assigned to "Group A" Is there something wrong with my
syntax?


IIf([Record_ID]>0<=2000,"Group A",IIf([Record_ID]
=2001<=3000,"Group B",IIf([Record_ID]>=3001<=4000,"Group
C",IIf([Record_ID]>=4001<=5000,"Group D","NA"))))


Thanks
Don
 
T

tina

try
IIf([Record_ID] > 4000, "Group D", IIf([Record_ID] > 3000, "Group C",
IIf([Record_ID] > 2000, "Group B", IIf([Record_ID] > 0, "Group A", "N/A"))))
note: if you have record ids over 5000, you'll have to use Between...And...
for all the statements, instead, as
IIf([Record_ID] Between 1 And 2000, "Group A", IIf( ....
suggest you also check out the Switch() function in Access Help, to see if
it might work better than nested IIf() functions.

hth


Don said:
Hello - I created this IIF function to evaluate a field
called Record_ID. I want to assign each record to a group
if it falls into a certain range. When I run the query
that contains this IIF function all my values are
Assigned to "Group A" Is there something wrong with my
syntax?


IIf([Record_ID]>0<=2000,"Group A",IIf([Record_ID]
=2001<=3000,"Group B",IIf([Record_ID]>=3001<=4000,"Group
C",IIf([Record_ID]>=4001<=5000,"Group D","NA"))))


Thanks
Don
 
W

Wayne Morgan

Tonin is correct, Switch would probably be easier. The Switch function will
use the first True value it finds, moving from Left to Right through the
list. Therefore, if you have <=2000 followed by <=3000, the >=2001 is
implied because it will never get to the second one if the first one is
true. To add in your >0 option, make it the first one and set the value to
"" or "Error", whichever you prefer. Modifying Tonin's function for this it
would be:

Switch([Record_ID]<=0, "Error", [Record_Id]<=2000,"Group
A",[Record_Id]<=3000,"Group B",[Record_Id]<=4000,"Group
C",[Record_Id]<=5000,"Group D",[Record_Id]>5000,"NA")

or

Switch([Record_ID]<=0, "", [Record_Id]<=2000,"Group
A",[Record_Id]<=3000,"Group B",[Record_Id]<=4000,"Group
C",[Record_Id]<=5000,"Group D",[Record_Id]>5000,"NA")


However, for future reference, there is a syntax problem.
IIf([Record_ID]>0<=2000,"Group A",IIf([Record_ID]

You have to spell out both parts if you want the value to meet two criteria.
In this case you want Record_ID to be greater than 0 and less than or equal
to 2000. This should be depicted as

IIf([Record_ID]>0 And [Record_ID]<=2000, "Group A", IIf(......

--
Wayne Morgan
Microsoft Access MVP


Don said:
Hello - I created this IIF function to evaluate a field
called Record_ID. I want to assign each record to a group
if it falls into a certain range. When I run the query
that contains this IIF function all my values are
Assigned to "Group A" Is there something wrong with my
syntax?


IIf([Record_ID]>0<=2000,"Group A",IIf([Record_ID]
=2001<=3000,"Group B",IIf([Record_ID]>=3001<=4000,"Group
C",IIf([Record_ID]>=4001<=5000,"Group D","NA"))))


Thanks
Don
 
T

Tonín

BTW, if you are working in design view, then change all commas "," into
semicolons ";"
:) as follows:

Switch([RecordId]<=2000;"Group A";[RecordId]<=3000;"Group
B";[RecordId]<=4000;"Group C";[RecordId]<=5000;"Group
D";[RecordId]>5000;"NA")

Regarding to your original "IIf" expression:
"IIf([Record_ID]>0<=2000;IIf(...

that is not OK. Try:
"IIf(([Record_ID]>0) And ([Record_ID]<=2000);IIf(... and so on.


Tonín

Tonín said:
Easier than IIf, let you use "switch"


Switch([RecordId]<=2000,"Group A",[RecordId]<=3000,"Group
B",[RecordId]<=4000,"Group C",[RecordId]<=5000,"Group
D",[RecordId]>5000,"NA")



Tonín

Don said:
Hello - I created this IIF function to evaluate a field
called Record_ID. I want to assign each record to a group
if it falls into a certain range. When I run the query
that contains this IIF function all my values are
Assigned to "Group A" Is there something wrong with my
syntax?


IIf([Record_ID]>0<=2000,"Group A",IIf([Record_ID]
=2001<=3000,"Group B",IIf([Record_ID]>=3001<=4000,"Group
C",IIf([Record_ID]>=4001<=5000,"Group D","NA"))))


Thanks
Don
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Aggregate Function Error 0
IIF/AND statement help 6
IIF Function 5
IIF Function in Update Query HELP! please :) 0
IIF Function 5
IIF function with * 7
Conditional Formula 2
Iif within Iif not working 3

Top