Hi EveryBody!
I am struck in a query that would return records of employees who are retiring
i On a given Date
ii Before a given Date
iii Between two given dates.
The Structure of the the input table is as below ( I have simplified it for the purpose in hand, actually it is a query based on three other subqueries in my database) -
employees_table :
EmpCode
Name
DateOfBirth
The rules of calculating a Retirement Date are -
- An employee would retire on last day of the month of his birth-date after 60 years if day of birth is greater than 1
Else he would retire on last day of the month preceeding the month of his birth-date.
Example for DOB: 25th April, 2000 RetirementDate is 30th April 2060
for DOB: 1st Jan, 2001 RetirementDate is 31st Dec. 2060
I am using the following VBA function for calculating Retiring Dates -
I have used the above function in a subquery that calculates the retiring dates for all employees - it works well, but when I try to use this subquery in another query to further filter the records by using a WHERE clause, as per three requirements stated above (one at time in a separate query), Access does not calculate it stating it to be complex and suggests to simlify it. The subquery and the main query are as under -
Access prompts to enter "GivenDate" which I supply to it but after a while it notifies its incapability.
Please Help.
Regards - PKJoshi.
I am struck in a query that would return records of employees who are retiring
i On a given Date
ii Before a given Date
iii Between two given dates.
The Structure of the the input table is as below ( I have simplified it for the purpose in hand, actually it is a query based on three other subqueries in my database) -
employees_table :
EmpCode
Name
DateOfBirth
The rules of calculating a Retirement Date are -
- An employee would retire on last day of the month of his birth-date after 60 years if day of birth is greater than 1
Else he would retire on last day of the month preceeding the month of his birth-date.
Example for DOB: 25th April, 2000 RetirementDate is 30th April 2060
for DOB: 1st Jan, 2001 RetirementDate is 31st Dec. 2060
I am using the following VBA function for calculating Retiring Dates -
Code:
Function RetiringDate(DOB As Date) As Date
Dim dob_day, dob_mnth, dob_yr, ret_day, ret_mnth, ret_yr As Long
dob_day = Day(DOB)
dob_mnth = Month(DOB)
dob_yr = Year(DOB)
If dob_day = 1 And dob_mnth = 1 Then
ret_mnth = 12
ret_yr = (dob_yr + 60) - 1
ElseIf dob_day = 1 Then
ret_mnth = dob_mnth - 1
ret_yr = (dob_yr + 60)
Else
ret_mnth = dob_mnth
ret_yr = (dob_yr + 60)
End If
If ret_mnth = 1 Or ret_mnth = 3 Or ret_mnth = 5 Or ret_mnth = 7 Or _
ret_mnth = 8 Or ret_mnth = 10 Or ret_mnth = 12 Then
ret_day = 31
ElseIf ret_mnth <> 2 Then
ret_day = 30
Else
ret_day = 28
End If
RetiringDate = CDate(ret_yr & "/" & ret_mnth & "/" & ret_day)
End Function
I have used the above function in a subquery that calculates the retiring dates for all employees - it works well, but when I try to use this subquery in another query to further filter the records by using a WHERE clause, as per three requirements stated above (one at time in a separate query), Access does not calculate it stating it to be complex and suggests to simlify it. The subquery and the main query are as under -
Code:
RetirementDates_sq:
SELECT EmpCode, Name, DateOfBirth, RetiringDate(DateOfBirth) As RetirementDate
FROM employee_table;
RetirementDates_fq:
SELECT EmpCode, Name, DateOfBirth, RetirementDate
FROM RetirementDates_sq
WHERE GivenDate = RetirementDate;
Access prompts to enter "GivenDate" which I supply to it but after a while it notifies its incapability.
Please Help.
Regards - PKJoshi.
Last edited: