I'm trying to do a Query that returns either child, adult or senior, based on
teh calculated age in the same query, column titled Age - I have the below
but I'm getting syntax errors - any suggestions ?
IIf([Age]<=21,"Child") Or IIf([Age]>21 And <60,"Adult") Or
IIf([Age]>=60,"Senior")
You need to include the criteria field in each part of the "And"
clause.
As there are only 3 possible choices, if the [Age] is not under 21 nor
between 21 and 60 it must be "Senior". No need to test for it.
Also your parenthesis placements were not correct.
NewColumn:IIf([Age]<=21,"Child",IIf([Age]>21 and [Age] <
60,"Adult","Senior"))
If [Age] is the name of a field in your table storing a person's age,
you are making a mistake It's bound to be incorrect at least once per
year.
It would be better to have a "Date of Birth" [DOB] field, then
calculate the age in the query. That way, the age will always be
correct.
To correctly calculate a person's age using a [DOB] field:
In a query:
Age: DateDiff("yyyy",[DOB],Date())-IIf(Format([DOB],
"mmdd")>Format(Date(),"mmdd"),1,0)
Directly as the control source of an unbound control in a report or on
a form:
=DateDiff("yyyy",[DOB],Date())-IIf(Format([DOB],
"mmdd")>Format(Date(),"mmdd"),1,0)
Where [DOB] is the birthdate field.