[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Access 2003 db Query for DOB

Posted on 2013-01-29
11
Medium Priority
?
261 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 52

Assisted Solution

by:Gustav Brock
Gustav Brock earned 2000 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
Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

 

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 52

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 52

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 52

Accepted Solution

by:
Gustav Brock earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

649 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