Results 1 to 7 of 7
  1. #1
    OB7 is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Oct 2013
    Posts
    16

    Formula to Calculate Age, as of a specific date, using a variable for the year

    My table contains customer information, including name, birthdate, order number, order date, and order year. I would like to create an Access query in Design View that will provide a customer list including each customer’s AGE as March 1 of the order year.





    If the order year is hard coded into the Age formula below, it works without any problem:

    Age: Int((#3/1/2017#-[birth_date])/365)


    However, I want to use the order_yr variable, as follows, but this results in an error:
    Age: Int((#3/1/[order_yr])#-[birth_date])/365)

    Any suggestions??

    Thanks!!!

  2. #2
    Minty is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,142
    You have to concatenate the field into the string. Something like

    Age : Int((DateValue("3/1/"&[OrderYear]) - [Birth_Date])/365)
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  3. #3
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,361
    IIRC, you can't use variables in a query field and that is what you asked for. You can use other field names, but to make use of a variable, you'd have to call a function or create your sql in code and run it from a procedure. What you posted looks more like field or form control names, so if they are, best to post back explaining what [Order Year] etc. is.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    OB7 is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Oct 2013
    Posts
    16
    Thank you, Minty! Your suggestion solved my problem!!

    However, in contrast to what I had stated in my post, the OrderYear is not in the TABLE but rather is derived from the OrderDate (which IS in the table) by way of a string function within the QUERY. I apologize for the incorrect information! Thus, when I ran the query using your formula, I received the following message:

    "This expression is typed incorrectly, or it is too complex to be evaluated. For example, a numeric expression may contain too many complicated elements. Try simplifying the expression by assigning parts of the expression to variables."

    However, if I first run my query (without an Age formula) as a MakeTable in order to place the OrderYear in the table, and then query THAT table with your Age formula, it works perfectly! If you know of a more efficient way to do this, rather than having to run two queries, please let me know. Otherwise, having to run two queries isn't so bad, and I'm VERY grateful for your solution to my problem!!

  5. #5
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,361
    I wouldn't consider running 2 queries to get the desired result to be unusual. However, I would consider creating a table first, in order to accomplish the goal, to be a sign of design problems. What are you going to do each time this needs to happen? Delete then recreate the same table? Not good practice, and some would say, a recipe for db bloat at least.

    If OrderYear is a calculated field in a query, you can still use it in other query calculations in additional fields, so I'm not understanding what the issue really is.

  6. #6
    Minty is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,142
    To re-inforce what Micron has said, in 99% of cases you shouldn't store anything you can calculate. Just like you would't store someones age (as it is a constantly changing value), if your Order_year is simply derived from an order date you can always get that by simply using Year(YourOrderDate)

    By always calculating you don't need to worry trying to capture if anyone ever changes a value in the underlying data.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  7. #7
    OB7 is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Oct 2013
    Posts
    16
    Minty and Micron,

    Thank you both for your help! Much appreciated!

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

Similar Threads

  1. Replies: 5
    Last Post: 01-04-2017, 02:13 AM
  2. Replies: 3
    Last Post: 02-16-2016, 05:02 PM
  3. Replies: 3
    Last Post: 09-14-2013, 10:36 AM
  4. Need a formula to Calculate Date Ranges
    By nagiese in forum Queries
    Replies: 5
    Last Post: 07-12-2013, 01:53 PM
  5. Replies: 8
    Last Post: 12-05-2012, 07:24 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