RE: detect overlapping time periods

From: Peter Martin (PeterMartin_at_discussions.microsoft.com)
Date: 09/16/04


Date: Thu, 16 Sep 2004 09:19:01 -0700

Allen Brown MVP has a nice explanation of a simpler method (using only a
query) at
http://members.iinet.net.au/~allenbrowne/appevent.html
but he uses a cartesian product which /may/ not be appropriate for your
application.

Regards, Peter.

"Peter Martin" wrote:

> HI,
>
> I have this function MultiCover which may be useful. You supply it a
> recordset made up of the start and
> end times of the intervals as separate records. You could make this with a
> union query.
>
> SELECT dtmStartTime as dbl, 1 as Ev FROM mytimes UNION SELECT dtmEndTime as
> dbl, -1 as Ev FROM mytimes ORDER BY dbl
>
> Then, if there was any overlap between your form and the database records
> you'd have
> MultiCover(rs, Form!myform!dtstart, Form!myform!dtEnd)>0.
>
> nMin, nMax are just min and max functions, something like
> Public Function Min(ParamArray la() As Variant) As Variant
> Dim l As Long
> If UBound(la) = 0 Then Min = Null: Exit Function
> Min = la(0)
> For l = 1 To UBound(la)
> Min = IIf(la(l) < Min, la(l), Min)
> Next l
> End Function
>
> Note it doesn't set rs=nothing because I used the rs elsewhere - its
> probably better for you to define the rs inside the function and pass SQL in
> your case.
>
>
> Public Function MultiCover(rs As DAO.Recordset, Optional ClipLo, Optional
> ClipHi) As Double
> 'Finds cover of set of intervals between optional clipping
> 'rs: set of period start/stop events format !dbl (ASC),!Ev {1,-1)
>
> Dim dblStart As Double, lStarts As Long, dblClipLo As Double, dblClipHi As
> Double
>
> dblClipLo = IIf(IsMissing(ClipLo), DBL_MIN, CDbl(ClipLo))
> dblClipHi = IIf(IsMissing(ClipHi), DBL_MAX, CDbl(ClipHi))
> If (IsMissing(ClipLo) Or IsMissing(ClipHi)) Then
> Else
> If dblClipHi < dblClipLo Then dblStart = dblClipHi: dblClipHi =
> dblClipLo: dblClipLo = dblStart 'Swap
> End If
>
> With rs
> If Not .BOF Then .MoveFirst
> Do While Not .EOF
> If !ev > 0 Then
> If lStarts = 0 Then dblStart = nMin(nMax(!dbl, dblClipLo), dblClipHi)
> lStarts = lStarts + 1
> Else
> lStarts = lStarts - 1
> If lStarts = 0 Then MultiCover = MultiCover + nMax(nMin(!dbl,
> dblClipHi), dblClipLo) - dblStart
> End If
> .MoveNext
> Loop
> End With
>
> End Function
>
> Well its one idea maybe wait for something better from the MVP's.
>
> Regards
>
> "mikebo" wrote:
>
> > I need to detect if two time periods overlap. I have multiple time periods
> > defined in a table with a start date and an end date. On a form, another
> > start and end date is defined. I need to find out if the period defined on
> > the form overlaps with any of the periods defined int he table. How do I do
> > that?
> >
> > I thought I had posted this before, but I can't see it, so here it is again.
> >
> >



Relevant Pages

  • Re: find missing time periods
    ... User and UserShiftStart in sample data, ... I believe your unmatched query actually ... FROM [Time Periods], Splits; ...
    (microsoft.public.access.queries)
  • Re: find missing time periods
    ... The Time Periods table is set up with 2 fields: ... , Date/Time ... etc in 1/2 hour increments to period 21 at 5:00:00 PM ... The preliminary query groups the data by date and time period. ...
    (microsoft.public.access.queries)
  • Re: find missing time periods
    ... I have a table with the Time Periods listed as well as a query with each ... The Unmatched Query won't work ... you might have "modeled" your Login as ...
    (microsoft.public.access.queries)
  • Re: Crosstab Query II
    ... The query result shows all 1/4 hour time periods from 8AM ... The final query data would give me all customers ... >-your current SQL view of the Crosstab ...
    (microsoft.public.access.queries)
  • Re: Year-to-date Totals
    ... You have a query that ... you want to get totals from for various time periods time. ... >> Joe Cilinceon ...
    (microsoft.public.access.queries)