Re: Mulitple IIf statements - Too many

Tech-Archive recommends: Fix windows errors by optimizing your registry



The database I was asking about originally has been put on hold. I tried to
use the SWITCH statement in a different database and I am getting an "#Error"
in the field. I want to have customer numbers in one range return one value
and customer in a second range return another value. If the customer number
does not fall within either of these two ranges, it should return the value
from the Division field in the table. Here is my statement. Can you see
what is wrong with it? Thank you.

RptDivision: Switch([CustomerNo] Between "159001" And "159699","ABS
Pharmacy",[CustomerNo] Between "156300" And "156999","SV
Pharmacy",True,[Division])

"John Vinson" wrote:

On Wed, 22 Nov 2006 10:09:02 -0800, AccessIM
<AccessIM@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

How many nested IIf statements are allowed in a single query field?

I have a list of 41 slot ranges with corresponding section numbers. I am
trying to use a query to say if the slot number for an item is between the
two slot range numbers, assign this section number. See my example below.

IIf([Slot] Between "030101" And "030192","401", IIf([Slot] Between "030701"
And "031192","402", IIf([Slot] Between "031301" And "031792","403",

I did this 41 times for each section with a FALSE value at the end and, when
I try to run the query, it says the query is too complex.

Does anyone have a better way to do this or any suggestions? Thank you so
much!

Klatuu's suggestion is an excellent one and will work, but there are a
couple of alternatives that you might want to consider.

For quick & dirty cases where there aren't *too* many alternatives,
the Switch() function is more compact and efficient than multiple
IIF's. It takes any number of arguments in pairs; the pairs are read
left to right, and when the function first finds that the first member
of the pair is TRUE it returns the second member of the pair and
quits:

Switch([Slot] BETWEEN "030101" And "030192","401",
[Slot] Between "030701" And "031192","402",
[Slot] Between "031301" And "031792","403",
....,
TRUE, "999")

Even better would be a table-driven solution. The IIF, Switch, and
even Klatuu's VBA code require you to dig into complex expressions if
the ranges or the returned value ever change. Instead, you can create
a Table named Sections:

Sections
SlotLow
SlotHigh
Section

with the obvious values; you can create a Query joining your table to
Sections using SQL like

SELECT mytable.thisfield, mytable.thatfield, Sections.Section\
FROM mytable
INNER JOIN Sections
ON mytable.[Slot] >= [Sections].[SlotLow]
AND mytable.[Slot] <= [Sections].[SlotHigh];


John W. Vinson[MVP]



.



Relevant Pages

  • Re: complex filter and calculations in access
    ... when using a subquery on the same table as the main query, ... switch to SQL view. ... of your database using the from address in this post. ...
    (microsoft.public.access.queries)
  • Re: Add tables and queries dialog does not show queries
    ... If I want to define a new query (or want to add a table ... and queries to use in your new query. ... database objects just displays all the tables, ... then switch back to the visual designer. ...
    (comp.databases.ms-access)
  • Form that does not have menu nor can be minimized
    ... Hi - I'm working in a database that someone else created. ... It's been a hassle because I'm testing stuff in a query ... minimize it or switch to design view or leave it open while working on ...
    (comp.databases.ms-access)
  • Re: Query
    ... One way would be to switch to the SQL view and copy the text into your email. ... Paste the text into the query. ... The users could import the query into their copy of the database. ...
    (microsoft.public.access.queries)
  • Re: Change Text to Memo
    ... Sorry I didn't initially mention that the database is elsewhere. ... Switch a given table field to Memo ... Function SwitchFieldType(sDb As String, sTableName As String, sFieldName ... Dim sSQL As String ...
    (microsoft.public.access.modulesdaovba)