I have a table that stores medicals for employees.
Employee's must attend a health assessment medical every 'X' number of years. In this table the fields are:
EmployeeID (a foreign key from tblEmployees)
MedDate (the date of the medical)
Result (keywords to determine the overall outcome of the assessment e.g. ok, sub-optimal, unfit)
Observations (memo with details of the medical report)
Since employees must have a medical every 'X' number of years, I'd like my database to tell me when a medical will expire. The 'X' is dependant on the employee's age, and result from the medical assessment.
My query is currently:
And GetMedicalExpiry is a function:
The problem I've discovered is my query actually shows the latest expiry date when I actually want the latest MedDate + years.
For example if a 30 year old had a medical 30/01/2015 that was Satisfactory, and another medical one month later 25/02/2015 that was Sub-Optimal, then my current query would display an expiry date of 30/01/2018, when I want is to show 25/02/2016 because the February one was that employee's latest medical.
Employee's must attend a health assessment medical every 'X' number of years. In this table the fields are:
EmployeeID (a foreign key from tblEmployees)
MedDate (the date of the medical)
Result (keywords to determine the overall outcome of the assessment e.g. ok, sub-optimal, unfit)
Observations (memo with details of the medical report)
Since employees must have a medical every 'X' number of years, I'd like my database to tell me when a medical will expire. The 'X' is dependant on the employee's age, and result from the medical assessment.
My query is currently:
Code:
SELECT tblMedicalRecords.EmployeeID, Max(GetMedicalExpiry([MedDate],[DOB],[Result])) AS Med_Exp
FROM tblEmployees INNER JOIN tblMedicalRecords ON tblEmployees.EmployeeID = tblMedicalRecords.EmployeeID
GROUP BY tblMedicalRecords.EmployeeID;
Code:
Function GetMedicalExpiry(medDate As Date, DOB As Date, strResult As Variant) As Date
Dim iAge As Integer
Dim i As Integer
iAge = age(DOB) ' Function that returns the age from a date of birth
Select Case iAge
Case 0 To 54 ' Ages 0 to 54 have medicals every 3 years
i = 3
Case 55 To 64 ' Ages 55 to 64 have medicals every 2 years
i = 2
Case 65 To 1000 ' Ages 65+ have medicals yearly
i = 1
End Select
Select Case strResult
Case Is = "Sub-Optimal" ' If medical is reported as sub-optimal, another
i = 1 ' medical must be done it 1 year
Case Is = "Unfit" ' If medical is reported as unfit, don't add any
i = 0 ' years
End Select
i = i * 12
GetMedicalExpiry = DateAdd("m", i, medDate)
End Function
The problem I've discovered is my query actually shows the latest expiry date when I actually want the latest MedDate + years.
For example if a 30 year old had a medical 30/01/2015 that was Satisfactory, and another medical one month later 25/02/2015 that was Sub-Optimal, then my current query would display an expiry date of 30/01/2018, when I want is to show 25/02/2016 because the February one was that employee's latest medical.