MS Access - Date after fourth birthday

萝らか妹 提交于 2019-12-24 07:47:04

问题


Hopefully a relatively simple query.

I'm using MS Access, I'm trying to work out from the date of birth, the 1st of September after their 4th birthday.

So if my DOB was 02/07/2015, it would return 01/09/2019.

If my DOB was 03/09/2015, it would return 01/09/2020.

Any help would be much appreciated.

Thanks


回答1:


You could define a function such as:

Function SchoolStart(dob As Date) As Date
    If DateSerial(Year(dob), 9, 1) < dob Then
        SchoolStart = DateSerial(Year(dob) + 5, 9, 1)
    Else
        SchoolStart = DateSerial(Year(dob) + 4, 9, 1)
    End If
End Function

Example:

?SchoolStart(#2015-07-02#)
01/09/2019 

?SchoolStart(#2015-09-02#)
01/09/2020 

Note that, with the current function logic, a birthday on the 1st September includes the child in that year's school intake:

?SchoolStart(#2015-09-01#)
01/09/2019 

Or, if you want to use the above logic directly in a query or control source for a form, you could use the following inline expression:

DateSerial(Year([YourDate])+IIf(DateSerial(Year([YourDate]),9,1)<[YourDate],5,4),9,1)

Here, [YourDate] is the field which contains the date on which the calculation is to be performed.



来源:https://stackoverflow.com/questions/53573388/ms-access-date-after-fourth-birthday

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!