I found this age calculation in FAQ faq181-85.
Age = DateDiff("yyyy", Birthdate, Enddate) + _
(Enddate < DateSerial(Year(Enddate), Month(Birthdate), _
Day(Birthdate)))
The code above determines age by subtracting the years from the dates in question and then adds a slight twist at the end. If EndDate is less than the current year birthday the expression evaluates to false which is a -1. So if you haven't had your birthday yet this year, subtract 1 from the number of years calculated by DateDiff.
To pull all that together for your form, try something like the following:
1. Define 5 text boxes, txt0to6, txt7to12, txt13to18,
txtAdult and txtEndDate (make invisible if desired).
2. I could only get this to work with Access predefined
functions so if you need an End Date other than Now(),
etc. you can set txtEndDate in your Form Load event code,
otherwise let it default to Now().
3. Use the DCount function to return counts for each of them
In general, it will look like this:
txt0to6 = DCount("DOBFieldName", "tblPersonalDetails", _
"Age() < " & 7)
However, you will need to substitute the whole expression
above for Age() including txtEndDate so it will look like
the code below, assuming Birthday is your fieldname:
txt0to6 = DCount("Birthday", "tblPersonalDetails", _
& "DateDiff(""yyyy"", Birthday, txtEndDate) + " _
& "(txtEndDate < DateSerial(Year(txtEndDate), _
& "Month(Birthday), Day(Birthday))) < " & 7)
If your fieldname is different from Birthday, just
substitute your fieldname for Birthday above and below.
Similarly, the others are below:
txt7to12 = DCount("Birthday", "tblPersonalDetails", _
& "DateDiff(""yyyy"", Birthday, txtEndDate) + " _
& "(txtEndDate < DateSerial(Year(txtEndDate), _
& "Month(Birthday), Day(Birthday))) < " & 13) _
- txt0to6
txt13to18 = DCount("Birthday", "tblPersonalDetails", _
& "DateDiff(""yyyy"", Birthday, txtEndDate) + " _
& "(txtEndDate < DateSerial(Year(txtEndDate), _
& "Month(Birthday), Day(Birthday))) < " & 19) _
- txt0to6 - txt7to12
txtAdult = DCount("Birthday", "tblPersonalDetails", _
& "DateDiff(""yyyy"", Birthday, txtEndDate) + " _
& "(txtEndDate < DateSerial(Year(txtEndDate), _
& "Month(Birthday), Day(Birthday))) > " & 18)
For txt7to12 and txt13to18 I did one Count and then
subtracted the younger counts saved in the other text
boxes.
4. Put the above in your Form Current processing if you will
be making changes to tblPersonalDetails or in the Form
Load or Form Open code if it will be static data. If you
put it in the Form Current code it will be recalculated
each time. I'm giving extra detail here because I don't
know what level of expertise you have in VBA.
Good Luck! Thanks for an interesting question! Please remember to give helpful posts the stars they deserve! This makes the post more visible to others in need!
![[thumbsup] [thumbsup] [thumbsup]](/data/assets/smilies/thumbsup.gif)