?
Solved

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

Posted on 2011-03-03
3
Medium Priority
?
315 Views
Last Modified: 2012-05-11
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
Comment
Question by:tlahpalli
[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
  • 2
3 Comments
 
LVL 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) earned 2000 total points
ID: 35031812
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 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 2000 total points
ID: 35031843
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 51

Expert Comment

by:Gustav Brock
ID: 35038750
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

Open in new window


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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
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 …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Suggested Courses

752 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