Results 1 to 7 of 7
  1. #1
    jamierbooth is offline Novice
    Windows 7 64bit Access 2000
    Join Date
    Nov 2013
    Location
    Bristol
    Posts
    12

    Dcount based on age

    Hi all. In a nutshell - I'm using an unbound text box on a form to show how many people are under 16 from the related table.



    I'm building a customer management system which will hold various bits about the person and their family make up. I already have an unbound text box showing the total household members by using a VBA Dcount in the On Load event of the form.
    Code:
    Me.txtMembers = DCount("*", "[tblHHmembers]", "[CustID] = " & CustID) + 1     '+1 to add the client on
    Can I further this code to then only show the currently under 16's by looking at the DoB from the tblHHmembers ?

    It has been suggested to me to use a query. I don't know SQL (still learning vba!) and if I do it with a query, I'd have to use the query builder grid, but don't know where to start with that way either. I just assumed it would be easier in VB?

    Cheers!
    Jamie.

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Sometimes when I want to show calculations I will display them in a subform and base the subform on its own query. Using the query builder to create a named query may help. You could create a "Totals" query, perhaps.

  3. #3
    jamierbooth is offline Novice
    Windows 7 64bit Access 2000
    Join Date
    Nov 2013
    Location
    Bristol
    Posts
    12
    Thanks but as I mentioned, I'm not sure where to start with creating a query (either SQL or with the builder) that can work out people under 16 in the first place!

  4. #4
    jamierbooth is offline Novice
    Windows 7 64bit Access 2000
    Join Date
    Nov 2013
    Location
    Bristol
    Posts
    12
    Anyone...?

    Quote Originally Posted by jamierbooth View Post
    Hi all. In a nutshell - I'm using an unbound text box on a form to show how many people are under 16 from the related table.

    I'm building a customer management system which will hold various bits about the person and their family make up. I already have an unbound text box showing the total household members by using a VBA Dcount in the On Load event of the form.
    Code:
    Me.txtMembers = DCount("*", "[tblHHmembers]", "[CustID] = " & CustID) + 1     '+1 to add the client on
    Can I further this code to then only show the currently under 16's by looking at the DoB from the tblHHmembers ?

    It has been suggested to me to use a query. I don't know SQL (still learning vba!) and if I do it with a query, I'd have to use the query builder grid, but don't know where to start with that way either. I just assumed it would be easier in VB?

    Cheers!
    Jamie.

  5. #5
    azhar2006's Avatar
    azhar2006 is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2012
    Posts
    528
    Hi Jamie
    Use something like this
    IIf([FieldName]=16;" ")

  6. #6
    lfpm062010 is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Location
    US
    Posts
    415
    To add the Criteria of under 16 to your existing code:
    Syntax of Dcount: Dcount ( COUNTFIELD, TABLENAME, CRITERIA )

    If [DoB] is just the number (like 16), use this

    Me.txtMembers = DDCount("*","[tblHHmembers]","[CustID] = " & [CustID] & " AND [DoB] < 16 ")+1

    If [DoB] is the Date field (like MM/DD/YYYY), use this

    Me.txtMembers = DCount("*", "[tblHHmembers]", "[CustID] = " & CustID & " AND DateDiff(""yyyy"", [DoB], Now()) < 16 ") + 1

  7. #7
    jamierbooth is offline Novice
    Windows 7 64bit Access 2000
    Join Date
    Nov 2013
    Location
    Bristol
    Posts
    12
    Quote Originally Posted by lfpm062010 View Post
    If [DoB] is the Date field (like MM/DD/YYYY), use this

    Me.txtMembers = DCount("*", "[tblHHmembers]", "[CustID] = " & CustID & " AND DateDiff(""yyyy"", [DoB], Now()) < 16 ") + 1
    This worked perfectly. Thanks a lot!!! (I took the +1 off the end though, as that was only there to include the cust in overall household size).


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

Similar Threads

  1. DCount
    By Newbie11 in forum Reports
    Replies: 4
    Last Post: 08-03-2012, 12:23 AM
  2. DCount
    By nsteenhaut in forum Queries
    Replies: 2
    Last Post: 10-04-2011, 05:00 PM
  3. DCount and If
    By Madmax in forum Access
    Replies: 3
    Last Post: 07-28-2011, 06:53 AM
  4. DCount and SQL
    By DSTR3 in forum Queries
    Replies: 3
    Last Post: 12-06-2010, 03:07 PM
  5. Help with Dcount
    By tozey in forum Programming
    Replies: 1
    Last Post: 08-10-2010, 10:53 AM

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