Solved

How do I use a begin date field to calculate total years up to now?

Posted on 2011-03-03
309 Views
I have two begin date fields.  i want to use the total years up to now in each separate field and make a list of years to select from to locate only people with so many years in each field.
0
Question by:tlahpalli
• 2

LVL 119

Assisted Solution

Rey Obrero earned 500 total points
run this query, change field names and table accordingly and post your comment

select [Name], [datefield],datediff("yyyy",[datefield],Date()) as noOfYears
from table x
0

LVL 119

Accepted Solution

Rey Obrero earned 500 total points
if you want more accurate year calculation, use this

SELECT [Name], HireDate, DateDiff("yyyy",[HireDate],Date())+(Date()<DateSerial(Year(Date()),Month([HireDate]),Day([HireDate]))) AS YearsOfService
FROM tblEmployees
0

LVL 49

Expert Comment

If you want it absolutely correct, you need to take into account those born on 29th Feb. Use this function:
``````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
``````

Then your query will look something like this:

Select
*,
AgeSimple([YourDateField1], Date()) As ExperienceOne,
AgeSimple([YourDateField2], Date()) As ExperienceTwo
From
tblYourTable
Where
AgeSimple([YourDateField1], Date()) >= [Years of First Experience]
And
AgeSimple([YourDateField2], Date()) >= [Years of Second Experience];

When you run the query, it will ask for the Years of Experience for the two fields.

/gustav
0

Featured Post

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …