IIF function

junkforhr

Board Regular
Joined
Dec 16, 2009
Messages
115
Office Version
  1. 365
Platform
  1. Windows
Hi guys,

I am new to Acces ( one week in fact). I've been searching to web about Access and have found the IIF function.

I have a column of data with customers ages. What I am trying to do in query is group these customers by age groupings.

I have tried this:

Aged 0 to 17: IIf([Customer age]>=0 And <=17,"0 to 17," ")

But it comes up with an error: "The expression you entered contains invalid syntax".

Being very new to Access, I am not sure what I am doing is possible.

Can some please provide some assistance
 
You could use Between.

AgeGroup:Iif([AgeField] Between 0 And 17, "0-17")

There is another option - create a table with all possible ages and corresponding age groups.

Something like this perhaps.

<table bgcolor="#ffffff" border="1" cellspacing="0"><caption>
</caption><thead><tr><th bordercolor="#000000" bgcolor="#c0c0c0">AgeGroup</th><th bordercolor="#000000" bgcolor="#c0c0c0">Age</th></tr></thead><tbody><tr valign="top"><td bordercolor="#c0c0c0">0-17</td><td bordercolor="#c0c0c0" align="right">0</td></tr><tr valign="top"><td bordercolor="#c0c0c0">0-17</td><td bordercolor="#c0c0c0" align="right">1</td></tr><tr valign="top"><td bordercolor="#c0c0c0">0-17</td><td bordercolor="#c0c0c0" align="right">2</td></tr><tr valign="top"><td bordercolor="#c0c0c0">0-17</td><td bordercolor="#c0c0c0" align="right">3</td></tr><tr valign="top"><td bordercolor="#c0c0c0">0-17</td><td bordercolor="#c0c0c0" align="right">4</td></tr><tr valign="top"><td bordercolor="#c0c0c0">0-17</td><td bordercolor="#c0c0c0" align="right">5</td></tr><tr valign="top"><td bordercolor="#c0c0c0">0-17</td><td bordercolor="#c0c0c0" align="right">6</td></tr><tr valign="top"><td bordercolor="#c0c0c0">0-17</td><td bordercolor="#c0c0c0" align="right">7</td></tr><tr valign="top"><td bordercolor="#c0c0c0">0-17</td><td bordercolor="#c0c0c0" align="right">8</td></tr><tr valign="top"><td bordercolor="#c0c0c0">0-17</td><td bordercolor="#c0c0c0" align="right">9</td></tr><tr valign="top"><td bordercolor="#c0c0c0">0-17</td><td bordercolor="#c0c0c0" align="right">10</td></tr><tr valign="top"><td bordercolor="#c0c0c0">0-17</td><td bordercolor="#c0c0c0" align="right">11</td></tr><tr valign="top"><td bordercolor="#c0c0c0">0-17</td><td bordercolor="#c0c0c0" align="right">12</td></tr><tr valign="top"><td bordercolor="#c0c0c0">0-17</td><td bordercolor="#c0c0c0" align="right">13</td></tr><tr valign="top"><td bordercolor="#c0c0c0">0-17</td><td bordercolor="#c0c0c0" align="right">14</td></tr><tr valign="top"><td bordercolor="#c0c0c0">0-17</td><td bordercolor="#c0c0c0" align="right">15</td></tr><tr valign="top"><td bordercolor="#c0c0c0">0-17</td><td bordercolor="#c0c0c0" align="right">16</td></tr><tr valign="top"><td bordercolor="#c0c0c0">0-17</td><td bordercolor="#c0c0c0" align="right">17</td></tr><tr valign="top"><td bordercolor="#c0c0c0">18-35</td><td bordercolor="#c0c0c0" align="right">18</td></tr><tr valign="top"><td bordercolor="#c0c0c0">18-35</td><td bordercolor="#c0c0c0" align="right">19</td></tr><tr valign="top"><td bordercolor="#c0c0c0">18-35</td><td bordercolor="#c0c0c0" align="right">20</td></tr><tr valign="top"><td bordercolor="#c0c0c0">18-35</td><td bordercolor="#c0c0c0" align="right">21</td></tr><tr valign="top"><td bordercolor="#c0c0c0">18-35</td><td bordercolor="#c0c0c0" align="right">22</td></tr></tbody><tfoot></tfoot></table>

This can then be used in 2 ways.

1 Add the table in a query and link the age fields.

2 Use DLookup, eg AgeGroup: DLookUp("[AgeGroup]","AgeGroups","[Age]=" & [CustAge])

SELECT Customers.CustName, Customers.CustAge, DLookUp("[AgeGroup]","AgeGroups","[Age]=" & [CustAge]) AS AgeGroup
FROM Customers;

Thanks as well Norrie. This idea is best suited to my needs.

Cheers
 
Upvote 0

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).

Forum statistics

Threads
1,224,815
Messages
6,181,135
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top