Public Function HolidayCheck(vStart As Date, vEnd As Date, vEmployeeID As Long, vHolidayID As Long) As Long
'Checks if new holiday period overlaps any existing holiday period, return 0 if no or return overlapped holiday period ID if yes
'Entry (vStart) = Start date of holiday period
' (vEnd) = End date of holiday period
' (vEmployeeID) = ID of employee to check
' (vHolidayID) = ID of record being checked (excluded from checks so it does not show it overlaps itself)
'Exit HolidayCheck = ID of holiday period record if new period overlaps or 0 if no overlap
Dim rst As Recordset
HolidayCheck = 0 'return 0 if no match found
Set rst = CurrentDb.OpenRecordset("SELECT * FROM tblHolidayDates WHERE EmployeeID = " & vEmployeeID & " AND HolidayID <> " & vHolidayID) 'make list of holiday dates except the one being changed
Do Until rst.EOF
If vStart <= rst!EndDate And vEnd >= rst!StartDate Then 'if new start <= existing end and new end >= existing start then
HolidayCheck = rst!HolidayID 'new holiday period overlaps this holiday so return ID of this holiday record
Exit Do 'no need to check any more
End If
rst.MoveNext 'next record
Loop
rst.Close
Set rst = Nothing
End Function