Solved

Access 2003 db Query for DOB

Posted on 2013-01-29
11
257 Views
Last Modified: 2013-02-16
Good day everyone. I have a query which contains a IIF() statement to find a DOB of a person on our database. I am use to using SQL and so i tried to use CAST() but that would not work.

How can I rewrite this query to equate the SQL query I have here:


CAST(floor((DateDiff(d,id.DOB,'2012-11-01')/365.25))  as INT) as AGE

This is for Access 2003
0
Comment
Question by:mig1980
  • 6
  • 4
11 Comments
 
LVL 77

Expert Comment

by:peter57r
ID: 38833421
int ((dateserial(2012,11,01) - ID.DOB)/365.25)   as AGE
0
 

Assisted Solution

by:mig1980
mig1980 earned 0 total points
ID: 38833447
I ended up using - DateDiff("yyyy", [Bdate], Now())+ Int( Format(now(), "mmdd") < Format( [Bdate], "mmdd") )

And it seems to work well. Do you see any flaws with using what I am using as appose to what you posted?
0
 
LVL 50

Assisted Solution

by:Gustav Brock
Gustav Brock earned 500 total points
ID: 38834378
Except that it will fail for certain cases.
Use an external function like the one below:

AgeSimple([Bdate]) As Age

Public Function AgeSimple( _
  ByVal datDateOfBirth As Date) _
  As Integer

' Returns the difference in full years from datDateOfBirth to current date.
'
' Calculates correctly for:
'   leap years
'   dates of 29. February
'   date/time values with embedded time values
'
' DateAdd() is used for check for month end of February as it correctly
' returns Feb. 28. when adding a count of years to dates of Feb. 29.
' when the resulting year is a common year.
' After an idea of Markus G. Fischer.
'
' 2007-06-26. Cactus Data ApS, CPH.

  Dim datToday  As Date
  Dim intAge    As Integer
  Dim intYears  As Integer
    
  datToday = Date
  ' Find difference in calendar years.
  intYears = DateDiff("yyyy", datDateOfBirth, datToday)
  If intYears > 0 Then
    ' Decrease by 1 if current date is earlier than birthday of current year
    ' using DateDiff to ignore a time portion of datDateOfBirth.
    intAge = intYears - Abs(DateDiff("d", datToday, DateAdd("yyyy", intYears, datDateOfBirth)) > 0)
  End If
  
  AgeSimple = intAge
  
End Function

Open in new window

/gustav
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mig1980
ID: 38836038
Can you explain to me on what cases it will fail? I know it won't account for leap year but what else?
0
 
LVL 50

Expert Comment

by:Gustav Brock
ID: 38836553
It is as noted in-line:

'   leap years
'   dates of 29. February
'   date/time values with embedded time values

In some cases this is not important at all (like sending birthday cards) but for insurance, employment, and similar it may be mandatory to have an exact measure.

/gustav
0
 

Author Comment

by:mig1980
ID: 38836589
How would you modify the function above if you wanted the age as of a certain date and you had the DOB in a database?
0
 
LVL 50

Expert Comment

by:Gustav Brock
ID: 38836649
For example:

Public Function AgeSimple( _
  ByVal datDateOfBirth As Date,
  ByVal datToday As Date) _
  As Integer

and then comment out:

' Dim datToday  As Date


And:

AgeSimple([Bdate], [SomeDate]) As Age

where [SomeDate] as a parameter of your query.

/gustav
0
 

Author Comment

by:mig1980
ID: 38836714
I won't be transferring the specific date parameter. It will be hard-coded. How can I hard-code it into the function for example: 10/1/2012
0
 
LVL 50

Accepted Solution

by:
Gustav Brock earned 500 total points
ID: 38837157
OK, then all you need is to specify that date in favour of today's date.
Change this line:

  datToday = Date

to:

  datToday = #10/01/2012#

/gustav
0
 

Author Comment

by:mig1980
ID: 38837197
Perfect. I didn't remember how to compose a specific date field in Access. It is the pound sign.

Thank you for your help. I will give this a try.
0
 

Author Closing Comment

by:mig1980
ID: 38896199
I ended up using my own answer but other answers were good as well.
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

680 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