Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to intialize the Datediff function? 2

Status
Not open for further replies.

HockeyFan

MIS
Jun 21, 2005
138
US
I am using a datediff function to calculate the number of work days between two dates.
The problem is that when the form opens, I seem to have to have a value in the fields that are part of the datediff function, or else I get an error becuase the fields are null. I also experience this problem when people use my form, but have only some of the fields that are used in this function, blank. For example, they might put in the begdate and not the end date.

Question: Could anyone help me with a simple line of code that I could put at the beginning of my datediff module that will evaluate, if the fields are null(blank), then don't perfom the datediff function? Here is my code that I am using. I figure that way, if they don't enter the begdate and the endate, then I won't try to run the funtion.


Function DateDiffW(BegDate, EndDate)
Const SUNDAY = 1
Const SATURDAY = 7
Dim NumWeeks As Integer

If BegDate > EndDate Then
DateDiffW= 0
Else
Select Case Weekday(BegDate)
Case SUNDAY : BegDate = BegDate + 1
Case SATURDAY : BegDate = BegDate + 2
End Select
Select Case Weekday(EndDate)
Case SUNDAY : EndDate = EndDate - 2
Case SATURDAY : EndDate = EndDate - 1
End Select
NumWeeks = DateDiff("ww", BegDate, EndDate)
DateDiffW= NumWeeks * 5 + Weekday(EndDate) - Weekday(BegDate)
End If
End Function
 
How about adding this line at the begining of the function
Code:
if not isdate(BegDate) and not isdate(EndDate) then exit function
 
HockeyFan . . .

... another way:
Code:
[blue]   Const SUNDAY = 1
   Const SATURDAY = 7
   Dim NumWeeks As Integer

   [purple][b]If IsDate(BegDate) And IsDate(EndDate) Then[/b][/purple]
      If BegDate > EndDate Then
         DateDiffW = 0
      Else
         Select Case Weekday(BegDate)
            Case SUNDAY: BegDate = BegDate + 1
            Case SATURDAY: BegDate = BegDate + 2
         End Select
         Select Case Weekday(EndDate)
            Case SUNDAY: EndDate = EndDate - 2
            Case SATURDAY: EndDate = EndDate - 1
         End Select
         NumWeeks = DateDiff("ww", BegDate, EndDate)
         DateDiffW = NumWeeks * 5 + Weekday(EndDate) - Weekday(BegDate)
      End If
   [purple][b]Else
      MsgBox "Check Your Dates!"
   End If[/b][/purple][/blue]

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Thank you both very much. it worked perfectly. I believe that anyone who uses this function should put that check in. Otherwise they would get the null value error as well. thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top