By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,390 Members | 1,579 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,390 IT Pros & Developers. It's quick & easy.

moved from the wrong location

P: n/a

From: "WindAndWaves" <ac****@ngaru.com>
Subject: Re: why use a class module
Date: Wednesday, 26 May 2004 6:25 p.m.

this is my code that takes a while to run:

I have put in the checks, because some fields may not contain proper date
fields or what have you.
Function fsimdat(Optional VFRO1, Optional VUNT1, Optional VFRO2, Optional
VUNT2) As Byte
'works out whether or not two date periods coincide
'0 = do not coincide
'1 = coincide
'2 = error
on error GoTo err
'--------------------------------------
Dim Fro1 As Date
Dim Unt1 As Date
Dim Fro2 As Date
Dim Unt2 As Date
'--------------------------------------
If IsNull(VFRO1) = True Or IsDate(VFRO1) = False Then GoTo data_error
Else Fro1 = VFRO1
If IsNull(VUNT1) = True Or IsDate(VUNT1) = False Then GoTo data_error
Else Unt1 = VUNT1
If IsNull(VFRO2) = True Or IsDate(VFRO2) = False Then GoTo data_error
Else Fro2 = VFRO2
If IsNull(VUNT2) = True Or IsDate(VUNT2) = False Then GoTo data_error
Else Unt2 = VUNT2
If Fro1 > Unt1 Or Fro2 > Unt2 Then GoTo data_error
If Fro1 = Unt1 Then Unt1 = Unt1 + 1
If Fro2 = Unt2 Then Unt2 = Unt2 + 1
'----------------------------'----------------------------
fsimdat = 2
'----------------------------'----------------------------
'1: |----|
'2: |---|
If Unt2 < Fro1 Then GoTo UnCoinciDe
'----------------------------'----------------------------
'1: |----|
'2: |---|
If Unt1 < Fro2 Then GoTo UnCoinciDe
'----------------------------'----------------------------
'1: |----|
'2: |-------|
If Fro1 >= Fro2 And Unt1 <= Unt2 Then GoTo CoinciDe
'----------------------------'----------------------------
'1: |---------|
'2: |-------|
If Fro2 >= Fro1 And Unt2 <= Unt1 Then GoTo CoinciDe
'----------------------------'----------------------------
'1: |------|
'2: |-----|
If Fro1 >= Fro2 And Unt1 >= Unt2 Then GoTo CoinciDe
'----------------------------'----------------------------
'1: |----|
'2: |-----|
If Fro1 <= Fro2 And Unt1 <= Unt2 Then GoTo CoinciDe

data_error:
fsimdat = 3
xit:
Exit Function
CoinciDe:
fsimdat = 1
GoTo xit
UnCoinciDe:
fsimdat = 0
GoTo xit
err:
fsimdat = 2
Resume Next
End Function
---
Please immediately let us know (by phone or return email) if (a) this email
contains a virus
(b) you are not the intended recipient
(c) you consider this email to be spam.
We have done our utmost to make sure that
none of the above are applicable. THANK YOU
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.690 / Virus Database: 451 - Release Date: 22/05/2004


---
Please immediately let us know (by phone or return email) if (a) this email
contains a virus
(b) you are not the intended recipient
(c) you consider this email to be spam.
We have done our utmost to make sure that
none of the above are applicable. THANK YOU
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.690 / Virus Database: 451 - Release Date: 22/05/2004
---
Please immediately let us know (by phone or return email) if (a) this email
contains a virus
(b) you are not the intended recipient
(c) you consider this email to be spam.
We have done our utmost to make sure that
none of the above are applicable. THANK YOU
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.690 / Virus Database: 451 - Release Date: 22/05/2004
Nov 13 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
You code is extremely confusing, but it seems like you're trying to
figure out if two date periods overlap.

All you need to check is whether the start or ending date of your
second period occur before the ending date of the first period.

Period 1
May 1, 2004 to May 4, 2004

Period 2
May 3, 2004 to May 10, 2004

Period 3
April 29, 2004 to May 2, 2004

All you need to check is whether either one of two conditions is
met:

Start2 is between Start1 and End1

OR

End2 is between Start1 and End1

So, what you need to test is a single conditional:

(Start2 >= Start1 And Start2 <= End1)
Or (End2 >= Start1 And Start2 <= End1)

This assumes that you know that of your four dates, two are starting
dates, and two are ending dates.

So, to wrap that in a Function:

Public Function CheckDateOverlap(Optional dteStart1 As Variant, _
Optional dteEnd1 As Variant, Optional dteStart2 As Variant, _
Optional dteEnd2 As Variant) As Integer
'0 = do not overlap
'1 = overlap
'2 = invalid input
Dim intOutput As Integer
Dim ysnResult As Boolean

If (IsMissing(dteStart1) Or IsMissing(dteStart1) _
Or IsMissing(dteStart1) Or IsMissing(dteStart1)) _
Or Not IsDate(dteStart1) Or Not IsDate(dteEnd1) _
Or Not IsDate(dteStart2) Or Not IsDate(dteEnd2)) Then
intOutput = 2
Else
intOutput = (dteStart2 >= dteStart1 And dteStart2 <= dteEnd1) _
Or (dteEnd2 >= dteStart1 And dteStart2 <= dteEnd1)
End If
CheckDateOverlap = Abs(intOutput)
End Function

You could add error handlers if you wanted, but I can't see what
could possibly go wrong here that would be a recoverable error.

If the dates are unordered, then you'd want to check them and put
them in ordered variables. If you're interested in that, I can add
that in a followup.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.