The codes are data, so are far better stored in a table than hard coded. Not
only does this conform with the fundamental 'information principle' of the
relational model (that all data are stored as values at column positions in
rows in tables and in no other way), but also at a practical level it makes
for easy maintenance. Create a table AgeCodes with columns LowerAge,
UpperAge and AgeCode. You can then join your 'people' table to the AgeCodes
table to return the code for each person, e.g.
SELECT FirstName, LastName, Age, AgeCode
FROM People INNER JOIN AgeCodes
ON People.Age >= AgeCodes.LowerAge
AND People.Age <= AgeCodes.UpperAge;
Don't be tempted to use a BETWEEN….AND operation in the JOIN clause; it
won't work in Access (though it does in standard SQL products). It can be
done as a join criterion in the WHERE clause, however:
SELECT FirstName, LastName, Age, AgeCode
FROM People, AgeCodes
WHERE People.Age BETWEEN AgeCodes.LowerAge
AND AgeCodes.UpperAge;
but this query would not be updatable.
You could also, albeit less efficiently, write a function which accepts the
age as an argument:
Function GetAgeCode (intAge as Integer) As String
Dim strCriteria As String
strCriteria = intAge & " >= LowerAge " & _
"And " & intAge & " <= UpperAge"
GetAgeCode = DLookup("AgeCode", "AgeCodes", strCriteria)
End Function
Ken Sheridan
Stafford, England