I have a table:
InactID-AutoNumber
PreceptorID-Number
InactStart-Date/Time
InactEnd-Date/Time
I don't want records that have overlapping time periods for a
PreceptorId. I tried:
[InactStart] < (DLookUp("[InactEnd]","tblInactive","[PreceptorID]=" &
[PreceptorID] & " and [InactID]=" & [InactID]-1))
but found that the DLOOKUP function was not permitted in a table validation rule.
The "table validation rule" is a misnomer (IMO) because it is actually
at the row level. You can use a Jet CHECK constraint (not well exposed
in the Access UI) which is truly table level.
If you periods are whole days then you could use the standard trick of
a Calendar table, then in your CHECK constraint join to this table
using GROUP BY on the calendar date e.g.
CHECK (NOT EXISTS (
SELECT C1.calendar_date
FROM EarningsHistory AS E1,
Calendar AS C1
WHERE C1.calendar_date BETWEEN E1.start_date
AND E1.end_date
GROUP BY E1.employee_id, C1.calendar_date
HAVING COUNT(*) > 1))
For a full demonstration see:
http://groups.google.com/group/microsoft.public.access.tablesdbdesign/msg/0fb58491ff039e1c
Otherwise, use a regular query in a table level CHECK constraint to
test for overlapping periods. The 'textbook' (Snodgrass's -- google
it) query for this (using closed-open representation) in Jet SQL
syntax:
SELECT S1.ssn
FROM SalaryHistory AS S1, Incumbents AS I1
WHERE S1.ssn = I1.ssn
AND IIF(S1.start_date > I1.start_date, S1.start_date, I1.start_date)
< IIF(S1.end_date > I1.end_date, I1.end_date, S1.end_date);
Obviously, you need to write this as a 'self join' for your
constraint. There are some decision to make along the way e.g.
representation (e.g. closed-closed or closed-open or something else),
the candidate keys on your table, ensuring end_date does not occur
before start_date, how to model the period in the current state
(end_date = NULL or end_date = <far future date representing infinity>
or something else), etc. Here's a long post from the google groups
archive which considers such issues:
http://groups.google.com/group/microsoft.public.access.forms/msg/04c3f233ba3336cc
Jamie.
--