Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    TOMMY.MYERS668's Avatar
    TOMMY.MYERS668 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Feb 2013
    Location
    West Virginia
    Posts
    77

    Age calc wrong.

    Ok I feel really stupid. I need to calculate age from dob in years so i can pull some data from another table.
    Should be:
    Age=datediff ( "yyyy" me! [dob],date)
    however this doesnt work ive aslo tried 1 y and doing it by days then devide by 365.25 , and also using now instead of date. The example I can give is
    Dob =7/27/1975


    The answe it gives me is 38 and it should be 37 because im pretty sure I know how old I am. Even worked it out on paper because at my sge you can forget things ...like whole years..lol please help

  2. #2
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    I think it is rounding off to the nearest whole year. Check out this link to see how to avoid that and just get full years: http://office.microsoft.com/en-us/ac...001055071.aspx

  3. #3
    TOMMY.MYERS668's Avatar
    TOMMY.MYERS668 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Feb 2013
    Location
    West Virginia
    Posts
    77

    Re: Age calc wrong.

    Ok tried that and get a compile error. Most like because I think thats for just creating a unbound text box. But I actually need to store the answer for later use. So im in an after update event and will need the vba formatting statement. Only have net on my phone right now so I'm trying to look that up. But yes your right I think its just rounding.

  4. #4
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Why not do the calculation in the underlying query, and just include that calculated field on your Report/Form?

  5. #5
    TOMMY.MYERS668's Avatar
    TOMMY.MYERS668 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Feb 2013
    Location
    West Virginia
    Posts
    77

    Re: Age calc wrong.

    Honestly because my knowledge of querys is non existent and I know vb pretty good so I just usually do everything with it. But this is just printing a formatted memo (report) and I need the age to reference a table for max weight.

  6. #6
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    You can't simply use DataDiff, as you've found out, because in essence, it merely subtracts one year from the other, in essence, and doesn't take into account whether of not the person has already had their birthday for the given year at that point in time! There are several formulas out there; here's one that I use:

    Code:
    Me.AgeToday = DateDiff("yyyy", [DOBField], Date()) - IIf(Format$(Date(), "mmdd") < Format$([DOBField], "mmdd"), 1, 0)


    Linq ;0)>

  7. #7
    TOMMY.MYERS668's Avatar
    TOMMY.MYERS668 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Feb 2013
    Location
    West Virginia
    Posts
    77

    Re: Age calc wrong.

    You sir are a saint. Works perfect! Ty

  8. #8
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Here is the formula contained in the link that I sent (it sounds like maybe you couldn't see it). It is somewhat similar to Linq's:
    Code:
    =DateDiff("yyyy", [Birthdate], Now())+ Int( Format(now(), "mmdd") < Format( [Birthdate], "mmdd"))
    Honestly because my knowledge of querys is non existent and I know vb pretty good so I just usually do everything with it. But this is just printing a formatted memo (report) and I need the age to reference a table for max weight.
    Queries are actually much easier than VBA, and one of the main reason to use Access. You can do calculations, filtering, linking to other tables, etc. You can also use queries as the source of Forms and Reports, just as you would with Tables.

    If you are going to use Access, you will definitely want to familiarize yourself with them. It will make your life a whole heck of a lot easier!

  9. #9
    TOMMY.MYERS668's Avatar
    TOMMY.MYERS668 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Feb 2013
    Location
    West Virginia
    Posts
    77

    Re: Age calc wrong.

    I actually started looking at them trying to help another guy on here. It just seems like for why I'm doing for the army right now theres not much use for them yet. But I'm trying to learn all I can.

  10. #10
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    It just seems like for why I'm doing for the army right now theres not much use for them yet.
    You might not just have a full understanding of them yet. They are usually one of the primary reasons to use Access in the first place! They are essential to relational databases.
    Otherwise, Excel would probably do (unless they only reason you are using Access is because of a size issue).

  11. #11
    TOMMY.MYERS668's Avatar
    TOMMY.MYERS668 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Feb 2013
    Location
    West Virginia
    Posts
    77

    Re: Age calc wrong.

    Actually im using it right now because I can perfectly format a military memo and prefill data into them by entering it on a general form. Then print 12 different memos for say 300 instructors all at once. But so much changes from memo to memo such as unit adress and.commanders signature blocks that doing 1 person in word takes about 2 hours. Does that make any sense or its there an easier way than access?

  12. #12
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    I see. So you probably have a Table as the Control Source of your Form. If you use a Query as the Control Source instead, you can perform many/all of your calculations in the query instead of doing it on the Form itself (I think they are easier to maintain there).

    Also, here is another good example of why you may want to base your Form on a Query instead of a Table. What if you only wanted the Form for a subset of your Instructors table (let's say you have a Location field on that table, and you only wanted to include a certain Location). With a query, you just add that Location value to the Criteria line of that Location field, and your Form will be restricted to just that Location.

  13. #13
    TOMMY.MYERS668's Avatar
    TOMMY.MYERS668 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Feb 2013
    Location
    West Virginia
    Posts
    77

    Re: Age calc wrong.

    Thats kind of how another one im doing is but the army has put that on hold till this one is done. And now I have a dlookup that isnt working but ill put that in a new post when I get home because I'm not typing all that code into my phone.

  14. #14
    TOMMY.MYERS668's Avatar
    TOMMY.MYERS668 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Feb 2013
    Location
    West Virginia
    Posts
    77
    Maybe I can get it in here:
    Me![max weight]= dlookup ("&me![agebracketpull]&","[height weight table]","[height]="&me![height]&")

    I get wrong number of arguements or invalid property assignment.

  15. #15
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Assuming your logic is correct, I think this is what you want:

    DLookUp("[agebracketpull]","[height weight table]","[height]=" & [height])

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Calc
    By hitman in forum Access
    Replies: 2
    Last Post: 02-14-2013, 07:10 AM
  2. Replies: 1
    Last Post: 06-04-2012, 07:17 PM
  3. Iff query with date calc?
    By technet in forum Queries
    Replies: 3
    Last Post: 02-08-2012, 10:17 AM
  4. calc field
    By nashr1928 in forum Forms
    Replies: 8
    Last Post: 11-09-2011, 09:21 PM
  5. Calc fields
    By nashr1928 in forum Forms
    Replies: 5
    Last Post: 02-26-2011, 08:35 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