All right, I'm going to write this out very methodically. Many of these steps can be compressed, but I'm going to treat them one at a time because that way I know the solution will, in fact, work.
First, create a temporary table. Here's the layout
Code:
tmptblEmpWeeks
ttKey autokey
EmpID foreign key to Emp
WeekNo Number 1 to 52
WeekDate Date (optional field)
WeekHours Number 0, 25, 40 or whatever
CumCode0 Number
CumCode1 Number
CumCode2 Number
CumCode3 Number
You will need code to clear out the temp table, then insert 1 record for each column (Week1 - Week52). That code is long but basic. CumCode0 will be 0 if Weekhours is 0, otherwise 1. Initialize the rest of the CumCode fields to 0.
Second, create a table that has this layout and insert the following fixed data into that table. This table simplifies the code for which weeks get compared to which other weeks:
Code:
tmptblCompWeek
ThisWeekNo Number
NextWeekNo Number
Fixed Data
ThisWeekNo NextWeekNo
1 2
2 3
3 4
4 5
... ...
50 51
51 52
Now, we'll start updating the CumCodes. First, CumCode1 will be used to determine if a particular week is the last week in a string of consecutive absences or presences.
Warning - this is aircode. Access is finnicky about the binds in update queries, and I've sometimes had to create temp tables to hold intermediate results, using a method like this.
Code:
DELETE from TempKeys;
SELECT TW1.TTKey Into TempKeys
FROM
tmptblEmpWeeks AS TW1
INNER JOIN
(tmptblEmpWeeks AS TW2
INNER JOIN
tmptblCompWeek AS TCW
ON TCW.NextWeekNo = TW2.WeekNo)
ON TCW.ThisWeekNo = TW1.WeekNo
AND TW1.EmpID = TW2.EmpID
WHERE
TW1.CumCode0 NOT EQUAL TW2.CumCode0;
UPDATE
tmptblEmpWeeks AS TW
INNER JOIN
TempKeys AS TK
ON TW.TTKey = TK.TTKey
SET
TW.CumCode1 = 1;
UPDATE
tmptblEmpWeeks AS TW
SET
TW.CumCode1 = 1
WHERE
WeekNo = 52;
Now, any employee week that is the last in a string of zero, or nonzero, weeks, has been marked with a 1 in CumCode1.
The next item will be used to mark each week with a code for which sequence it belongs to. It happens to be easier to do in descending sequence (where week 52 is designated group 1, the next earlier group is group 2, and so on). This query gets the values for each record:
Code:
qryCumCode2
SELECT TW1.EmpNo, TW1.TTKey, First(TW1.WeekNo) AS WeekNo, First(TW1.CumCode0) As CumCode0, Sum(TW2.CumCode1) AS GroupNo
FROM
tmptblEmpWeeks AS TW1
INNER JOIN
tmptblEmpWeeks AS TW2
ON TW1.EmpID = TW2.EmpID
WHERE
TW1.WeekNo <= TW2.WeekNo
GROUP BY TW1.EmpNo, TW1.TTKey;
I was intending to store those values in the tem p record, but I think we can get away with just using queries. We'll see.
Now, you can just sum the results off that query in a different order to get your number of weeks in a row for that group.
Code:
qryCumCode3
SELECT QC.EmpNo, QC.GroupNo, Max(QC.WeekNo) As CumWeekNo, First(QC.CumCode0) As CumCode0, Count(*) AS GroupCount
FROM qryCumCode2 as QC
GROUP BY QC.EmpNo, QC.GroupNo
ORDER BY QC.EmpNo, QC.GroupNo;
And that query gives you everything you absolutely need to answer your question. This should get you a list of the Employee Number, and the number of his last vacation/out week, where he did not work at least that many weeks in the next group.
Code:
qryCumCode4
SELECT
QC1.EmpNo,
QC1.GroupNo,
QC1.CumCode0,
QC1.CumWeekNo As OutWeekNo,
QC1.GroupCount AS OutCount,
QC2.CumWeekNo AS InWeekNo,
QC2.GroupCount AS InCount
FROM
qryCumCode3 as QC1
INNER JOIN
qryCumCode3 as QC2
ON QC1.EmpNo = QC2.EmpNo
AND QC1.GroupNo = QC2.GroupNo + 1
WHERE QC1.CumCode0 = 0
AND QC1.GroupCount > QC2.GroupCount;