[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Access 2003 - Find the age using datediff

Posted on 2007-10-05
25
Medium Priority
?
943 Views
Last Modified: 2008-05-24
I have Access 2003 - What I am trying to do is pull out anyone who is at a certain age when a proceudre was done. For example: jane Doe was 17 on this date. I have the birthdate and the date of service.
I am using this is the query AgeAtProcedure: (DateDiff("yyyy",[PatientBirthDate],[FromDate]))
It is returnign the age as example - born 10-22-89 and date of service of 10-1-1989 as 18 - I want it to be 17 because at 10-1-1989 they are still 17. I don't want it to show 18 until 10-22-2007.
0
Comment
Question by:Pdeters
  • 14
  • 5
  • 4
  • +1
24 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 20021815
This should give you the exact age:

cdbl(DateDiff("d",[PatientBirthDate],[FromDate]))/365
0
 

Author Comment

by:Pdeters
ID: 20021861
what about leap year - If the date of service is close to hteir birthdate of turning 18 (minor) it doesn't catch that one
0
 
LVL 93

Accepted Solution

by:
Patrick Matthews earned 1000 total points
ID: 20021954
Hello Pdeters,

Have a look at the YearsMonthsDays function described here:
http://vbaexpress.com/kb/getarticle.php?kb_id=866

You could use it like this:

SELECT Val(YearsMonthsDays([PatientBirthDate], [FromDate], True)) AS PatientAge
FROM SomeTable

Regards,

Patrick
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 58
ID: 20021965
Paste the function below into a module.  Then call from your query.  I assumed your dates where strings.

AgeAtProcedure:Age([PatientBirthDate],[FromDate])


JimD

Function age(varBirth As Variant, varService As Variant)

  Dim varAge As Variant
 
  varAge = DateDiff("yyyy", DateValue(varBirth), DateValue(varService))
  If Month(DateValue(varBirth)) < Month(DateValue(varService)) Then
    varAge = varAge - 1
   ElseIf Month(DateValue(varBirth)) = Month(DateValue(varService)) And Day(DateValue(varBirth)) < Day(DateValue(varService)) Then
     varAge = varAge - 1
  End If
   
  age = CInt(varAge)

End Function
0
 
LVL 58
ID: 20021975
<<I assumed your dates where strings.>>

  Note if they are actual date/time fields, you can pull out all the DateValue()'s in the function.

JimD
0
 

Author Comment

by:Pdeters
ID: 20022076
yes they are actual date/time fields.
When you say "call" from my query - would i be calling age ?
0
 

Author Comment

by:Pdeters
ID: 20022083
Sorry again - would I also just take out all of htese then in the module.
DateValue(varBirth), DateValue(varService)
0
 
LVL 58
ID: 20022145
<<When you say "call" from my query - would i be calling age ?>>

  yes, you'd be calling the function Age()

AgeAtProcedure:Age([PatientBirthDate],[FromDate])

<<Sorry again - would I also just take out all of htese then in the module.
DateValue(varBirth), DateValue(varService)>>

  Would look like this then:

Function age(varBirth As Variant, varService As Variant)

  Dim varAge As Variant
 
  varAge = DateDiff("yyyy", varBirth, varService)
  If Month(varBirth) < Month(varService) Then
    varAge = varAge - 1
   ElseIf Month(varBirth) = Month(varService) And Day(varBirth) < Day(varService) Then
     varAge = varAge - 1
  End If
   
  age = CInt(varAge)

End Function
0
 

Author Comment

by:Pdeters
ID: 20022335
I have created the module with the function. When i put it in the query i get this

Undefined function 'age' in expression.
0
 
LVL 58
ID: 20022431

  Make sure the module name is not called 'Age".

JimD

0
 

Author Comment

by:Pdeters
ID: 20022597
I did have it called age -

The totals are still not coming up correctly.
1-5-1989 to 6-25-2007 is showing 17
11-10-1989 o 4-26-2007 - is showing 18
should be the other way around.
0
 
LVL 58

Assisted Solution

by:Jim Dettman (Microsoft MVP/ EE MVE)
Jim Dettman (Microsoft MVP/ EE MVE) earned 1000 total points
ID: 20022710
<<should be the other way around.>>

  Sorry.

JimD

Function age(varBirth As Variant, varService As Variant)

  Dim varAge As Variant
 
  varAge = DateDiff("yyyy", varBirth, varService)
  If Month(varBirth) > Month(varService) Then
    varAge = varAge - 1
   ElseIf Month(varBirth) = Month(varService) And Day(varBirth) > Day(varService) Then
     varAge = varAge - 1
  End If
   
  age = CInt(varAge)

End Function
0
 

Author Comment

by:Pdeters
ID: 20022741
Thank you so  much for taking the time. I learned a lot here and I think it stuck.

thanks again
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 20022753
Pdeters,

Please download this file for an example using my suggestion:

Direct link to your file
<https://filedb.experts-exchange.com/incoming/ee-stuff/4937-Example.zip


Regards,

Patrick
0
 

Author Comment

by:Pdeters
ID: 20022755
THanks -
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 20022849
Pdeters,

Did you even try to implement my suggestion before you accepted Jim's?

Regards,

Patrick
0
 

Author Comment

by:Pdeters
ID: 20022950
Sorry I did not - I will try it right now -
0
 

Author Comment

by:Pdeters
ID: 20023124
I get a data type mismatch when I try and pull out a certain age.
any ideas what I need t change
0
 

Author Comment

by:Pdeters
ID: 20023126
It does give me the correct answer.
0
 

Author Comment

by:Pdeters
ID: 20023167
I copied downloaded your example file - and that is owrking fine when you choose criteria - I will work with mine more. And change how i credited the points. Sorry I did ot look at your solution.
0
 

Author Comment

by:Pdeters
ID: 20023200
thanks again it is working great
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 20023747
Pdeters,

Thanks for that.  When the Q does get reopened, I have zero objection to a split with Jim,
BTW...

Regards,

Patrick
0
 

Author Comment

by:Pdeters
ID: 20024514
Thanks again - and am really sorry

0
 

Author Comment

by:Pdeters
ID: 20024529
THanks
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
In a use case, a user needs to close an opened report by simply pressing the Escape (Esc) key. This can be done by adding macro code in Report_KeyPress or Report_KeyDown event.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question