Results 1 to 4 of 4
  1. #1
    Richie27 is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Feb 2012
    Location
    Ireland
    Posts
    159

    Extract age profiles of people

    Hi All,

    I have a database of people with various details about them, including their date of birth, in one table.

    I need to extract a report from this database which shows an age profile of all the people that are in it - maybe like a bar chart or perhaps a list of all the age ranges like:

    Age 10-20: 15 people


    Age 20-30: 19 people
    Age 30-40: 30 people

    Any ideas on how to approach this would be great.

    My little brain is thinking arrays and loops will be needed............not yet sure how they work in vba yet though!

  2. #2
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    No need for VBA. Start by creating a table that has the age groups

    tblAgeGroups
    -pkAgeGrpID primary key, autonumber
    -txtAgeGroupName (like your Age 10-20)
    -longLowerLimit
    -longUpperLimit

    The records in the above table would look like this:

    pkAgeGrpID|txtAgeGroupName|longLowerLimit|longUppe rLimit
    1|Age 10-20|10|20
    2|Age 20-30|20|30
    etc.

    Create a query that determines the age of each person. In that query you will need to nest another query that lookups up the appropriate age group

    query name: qryPeopleAgeGroup
    SELECT datediff("m",dteDOB, date())/12 as CurrentAge, (Select pkAgeGrpID FROM tblAgeGroups WHERE datediff("m",dteDOB, date())/12 >=longLowerLimit AND datediff("m",dteDOB, date())/12<longUpperLimit) as AgeGroup
    FROM tblPeople;


    Next create a query based on the above that groups and counts by AgeGroup

    query name: qryCountByAgeGroup
    SELECT qryPeopleAgeGroup.AgeGroup, Count(qryPeopleAgeGroup.AgeGroup) AS CountOfAgeGroup
    FROM qryPeopleAgeGroup
    GROUP BY qryPeopleAgeGroup.AgeGroup;


    Now assuming that you want to show a count for every age group even if the count is zero then you would need 1 final query. This requires a LEFT JOIN and a couple of built-in functions that are nested together

    query name: qryFinal

    SELECT tblAgeGroups.txtAgeGroupName, IIF(isnull(qryCountByAgeGroup.CountOfAgeGroup),0,q ryCountByAgeGroup.CountOfAgeGroup) as AgeGroupCount
    FROM tblAgeGroups LEFT JOIN qryCountByAgeGroup ON tblAgeGroups.pkAgeGrpID = qryCountByAgeGroup.AgeGroup;


    I've attached an example database as well.
    Attached Files Attached Files

  3. #3
    Richie27 is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Feb 2012
    Location
    Ireland
    Posts
    159
    Wow, another great one! Never thought of making a table with those values!!!

    Hmmmm, I must learn to think outside the box!!

    Thanks again for the invaluable lesson!

    Rich

  4. #4
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    You're welcome.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Hello Access People
    By redbull in forum General Chat
    Replies: 2
    Last Post: 03-22-2012, 12:06 PM
  2. Replies: 1
    Last Post: 02-09-2012, 11:49 AM
  3. Creating User Profiles in Access 2007
    By Ganymede in forum Security
    Replies: 3
    Last Post: 01-06-2012, 04:14 PM
  4. Number of people able to Access DB
    By noelrobb in forum Forms
    Replies: 4
    Last Post: 02-07-2011, 08:01 AM
  5. Replies: 0
    Last Post: 12-28-2009, 12:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums