Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Leonidsg is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2013
    Posts
    68

    Calculation Field in table vs Query calculation


    Hello I have a question on calculations based on dates in a table. I have two dates: Date of Birth: [DOB], and Date of Diagnosis: [DateDx]. I want a third field to calculate Age at Diagnosis by taking the difference between those two. It would be best if I could display that number in years and months. So I was wondering:
    1. Do I use a calculation field in a table or a query instead?
    2. If I use a query does this stress the system too much?
    3. With a query I tried a DateDiff("yyyy", [DOB], [DateDx]) equation but it is saying there is a syntax error, comma is/isn't where is should be. Also using this I would only get years not years and months.

    Any help with be great thank you.

    Lenny

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Try using this in your query:

    DateDiff("yyyy", [DateDx], Date())+ Int( Format(Date(), "mmdd") < Format( [DateDx], "mmdd") ) As TheAge

  3. #3
    SteveH2508 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Sep 2010
    Location
    Chelsea, London, England
    Posts
    117
    As I say to my students - it is a computer - it is designed to compute things - it does it way better and way faster than you can!

    Don't worry about stressing the system - it will barely notice a calculation like this.

  4. #4
    ketbdnetbp is offline Competent Performer
    Windows 7 32bit Access 2003
    Join Date
    Mar 2011
    Location
    Midwest
    Posts
    254
    Leonidsg -

    I agree with the OP, the calculation is best suited in a query. As a follow-up to RG's suggestion that addresses this issue based upon the current date, I encountered a similiar situation relative to age (years and months), as of a specific date for a 401(k) calculation; I used the following approach and it may fit your needs. Create a query based upon the table that contains those dates.

    In a new column of that query, add the following to the field line (returns total months between the dates):
    Months: IIf((DatePart("d",[DOB])<=DatePart("d",[DateDx])),(Int(DateDiff("m",[DOB],[DateDx]))),(Int(DateDiff("m",[DOB],[DateDx])-1)))

    Then, in another new column, add this to the field line (this will return months not divisible by 12 , i.e. remaining months):
    RM: Int([Months] Mod 12)

    Last, in another new column, add this to the field line (just to make it an easier read):
    AgeAtDiagnosis: Int([Months]/12) & " Years, " & [RM] & " Month(s)"

    I used three columns for illustrative purposes. There are probably ways to use fewer columns but, it was easier for me to use three. Test to see if this gets you the desired results.

    All the best,

    Jim

  5. #5
    Leonidsg is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2013
    Posts
    68

    a little more help please

    Quote Originally Posted by RuralGuy View Post
    Try using this in your query:

    DateDiff("yyyy", [DateDx], Date())+ Int( Format(Date(), "mmdd") < Format( [DateDx], "mmdd") ) As TheAge


    I still need a little help. I have two variables DOB and DateOfSurgery. Where do I put the DateofSurgery variable in the above equation. Thank you for all your help.

    I didn't see Ketbdnetbp post, let me try that first. Thanks again for all the help!!!

  6. #6
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    You should just be able to replace [DateDx] with [DateofSurgery].

  7. #7
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Actually, I don't think that is correct. I need to think about it for a minute.

  8. #8
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    I think it is going to be:
    DateDiff("yyyy", [DateOfSurgery], [DOB])+ Int( Format([DOB], "mmdd") < Format( [DateOfSurgery], "mmdd") ) As TheAge
    ...but I might have one of the dates backwards.

  9. #9
    Leonidsg is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2013
    Posts
    68

    Extra ) in the last part

    So when I am trying to use your formula everything is fine until I input AgeAtDiagnosis: Int([Months]/12) & " Years, " & [RM] & " Month(s) I am told that there is an extra ")" in the equation. Any suggestions.

    Thank you

    Quote Originally Posted by ketbdnetbp View Post
    Leonidsg -

    I agree with the OP, the calculation is best suited in a query. As a follow-up to RG's suggestion that addresses this issue based upon the current date, I encountered a similiar situation relative to age (years and months), as of a specific date for a 401(k) calculation; I used the following approach and it may fit your needs. Create a query based upon the table that contains those dates.

    In a new column of that query, add the following to the field line (returns total months between the dates):
    Months: IIf((DatePart("d",[DOB])<=DatePart("d",[DateDx])),(Int(DateDiff("m",[DOB],[DateDx]))),(Int(DateDiff("m",[DOB],[DateDx])-1)))

    Then, in another new column, add this to the field line (this will return months not divisible by 12 , i.e. remaining months):
    RM: Int([Months] Mod 12)

    Last, in another new column, add this to the field line (just to make it an easier read):
    AgeAtDiagnosis: Int([Months]/12) & " Years, " & [RM] & " Month(s)"

    I used three columns for illustrative purposes. There are probably ways to use fewer columns but, it was easier for me to use three. Test to see if this gets you the desired results.

    All the best,

    Jim

  10. #10
    Leonidsg is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2013
    Posts
    68
    Okay i think i was able to figure out the parentheses but now it prompts me for a parameter value for Months and RM? Thank you


    Quote Originally Posted by ketbdnetbp View Post
    Leonidsg -

    I agree with the OP, the calculation is best suited in a query. As a follow-up to RG's suggestion that addresses this issue based upon the current date, I encountered a similiar situation relative to age (years and months), as of a specific date for a 401(k) calculation; I used the following approach and it may fit your needs. Create a query based upon the table that contains those dates.

    In a new column of that query, add the following to the field line (returns total months between the dates):
    Months: IIf((DatePart("d",[DOB])<=DatePart("d",[DateDx])),(Int(DateDiff("m",[DOB],[DateDx]))),(Int(DateDiff("m",[DOB],[DateDx])-1)))

    Then, in another new column, add this to the field line (this will return months not divisible by 12 , i.e. remaining months):
    RM: Int([Months] Mod 12)

    Last, in another new column, add this to the field line (just to make it an easier read):
    AgeAtDiagnosis: Int([Months]/12) & " Years, " & [RM] & " Month(s)"

    I used three columns for illustrative purposes. There are probably ways to use fewer columns but, it was easier for me to use three. Test to see if this gets you the desired results.

    All the best,

    Jim

  11. #11
    ketbdnetbp is offline Competent Performer
    Windows 7 32bit Access 2003
    Join Date
    Mar 2011
    Location
    Midwest
    Posts
    254

    Age at Diagnosis Calculation

    Leonidsg -

    Cannot replicate the problem as the query seems to work for me. However, because [Months] and [RM] are dependent, I would double check the syntax in the [Months] field. It should be:

    Months: IIf((DatePart("d",[DOB])<=DatePart("d",[DateDx])),(Int(DateDiff("m",[DOB],[DateDx]))),(Int(DateDiff("m",[DOB],[DateDx])-1)))

    ...Change field names if needed.

    The SQL for the query that works for me is:

    SELECT Table4.ID, Table4.Name, Table4.[DOB], Table4.DateDx, IIf((DatePart("d",[DOB])<=DatePart("d",[DateDx])),(Int(DateDiff("m",[DOB],[DateDx]))),(Int(DateDiff("m",[DOB],[DateDx])-1))) AS Months, Int([Months] Mod 12) AS RM, Int([Months]/12) & " Years, " & [RM] & " Month(s)" AS AgeAtDiagnosis
    FROM Table4;

    I have also attached my test db, hope this helps,

    Jim
    Attached Files Attached Files

  12. #12
    Leonidsg is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2013
    Posts
    68

    How do I make the results appear in the original table

    Hey Jim I very much appreciate your help. I downloaded your database and looked at mine and I was able to get the same result. However I would like the Age to appear in the original table, so in your example that would be table 4. Any suggestions of how to add this to the original table? Or am I confused.

    Thank you
    Lenny



    Quote Originally Posted by ketbdnetbp View Post
    Leonidsg -

    Cannot replicate the problem as the query seems to work for me. However, because [Months] and [RM] are dependent, I would double check the syntax in the [Months] field. It should be:

    Months: IIf((DatePart("d",[DOB])<=DatePart("d",[DateDx])),(Int(DateDiff("m",[DOB],[DateDx]))),(Int(DateDiff("m",[DOB],[DateDx])-1)))

    ...Change field names if needed.

    The SQL for the query that works for me is:

    SELECT Table4.ID, Table4.Name, Table4.[DOB], Table4.DateDx, IIf((DatePart("d",[DOB])<=DatePart("d",[DateDx])),(Int(DateDiff("m",[DOB],[DateDx]))),(Int(DateDiff("m",[DOB],[DateDx])-1))) AS Months, Int([Months] Mod 12) AS RM, Int([Months]/12) & " Years, " & [RM] & " Month(s)" AS AgeAtDiagnosis
    FROM Table4;

    I have also attached my test db, hope this helps,

    Jim

  13. #13
    ketbdnetbp is offline Competent Performer
    Windows 7 32bit Access 2003
    Join Date
    Mar 2011
    Location
    Midwest
    Posts
    254
    Leonidsg -

    Unless there is a compelling reason to the contrary, calculated fields are best suited in a query. On the db I sent to you, the calculation is in the query named "Table4 Query1". However, if it is absolutely necessary, add a text field to the original table named AgeatDiagnosis and perform the calculation at the form level using VBA to set it's value after the the DateDX is updated.

    All the best,

    Jim
    Last edited by ketbdnetbp; 02-15-2013 at 08:52 PM. Reason: text field, not date field

  14. #14
    Leonidsg is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2013
    Posts
    68
    Jim,

    So when people are looking at the data is the best way for them to see the calculation from the field in a report? The people who will actually will use the database will not know to look at the query table to find the calculated field.

    Thank you,
    Lenny

  15. #15
    ketbdnetbp is offline Competent Performer
    Windows 7 32bit Access 2003
    Join Date
    Mar 2011
    Location
    Midwest
    Posts
    254
    Leonidsg -

    The calculation is conducted in the query. However, the result can be pulled from the query and used in a form, report, or elsewhere as needed. Display and use of its value really depends on where it serves the business purpose best.

    Jim

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

Similar Threads

  1. Calculation Field Issues
    By MintChipMadness in forum Access
    Replies: 7
    Last Post: 06-25-2012, 02:37 PM
  2. Calculation field
    By johnny in forum Access
    Replies: 4
    Last Post: 08-10-2011, 06:52 AM
  3. Simple field calculation
    By stryder09 in forum Access
    Replies: 4
    Last Post: 02-11-2011, 11:48 AM
  4. Subreport Calculation field
    By Cheshire101 in forum Reports
    Replies: 2
    Last Post: 01-14-2010, 05:50 PM
  5. Calculation field in form
    By ste_pie87 in forum Access
    Replies: 1
    Last Post: 03-24-2009, 08:28 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