View Full Version : date diff in validation function (weekdays only)


thisisntwally
12-10-2007, 04:43 AM
i've been away from access and my database for a month or two, and upon my return I've found myself a little baffled.

I've got a date validation in a form as so:
If ((vFrm!txtSSD - vFrm!txtDOR) > 3 And Nz(vFrm!txtComments, "") = "") Then
MsgBox "Turnaround Time is greater than 3 days, please comment."
amdValid = False
End If
but i only want to count weekdays. I've found a few examples searching datediff and weekdays, but I'm kinda lost on how to integrate the two. This is probably because I don't exactly understand how the following code is working. I think i need to call the workingdays() in place of vFrm!txtSSD - vFrm!txtDOR and call txtssd and txtdor as the start and end dates within the workingdays function, but I'm not exactly sure. Could anyone confirm/disconfirm this, or usher me along another road?
Public Function WorkingDays(StartDate As Date, EndDate As Date) As Integer

On Error GoTo Err_WorkingDays

Dim intCount As Integer

StartDate = StartDate + 1

intCount = 0
Do While StartDate <= EndDate
'Make the above < and not <= to not count the EndDate

Select Case WeekDay(StartDate)
Case Is = 1, 7
intCount = intCount
Case Is = 2, 3, 4, 5, 6
intCount = intCount + 1
End Select
StartDate = StartDate + 1
Loop
WorkingDays = intCount

Exit_WorkingDays:
Exit Function

Err_WorkingDays:
Select Case Err

Case Else
MsgBox Err.Description
Resume Exit_WorkingDays
End Select

End Function

ajetrumpet
12-10-2007, 06:25 AM
Wally (sorry if this is not you),

I posted my own function for calculating WeekDays here...

http://www.access-programmers.co.uk/forums/showthread.php?t=140131

Somebody else wanted to know about it. Maybe it can help you...

thisisntwally
12-11-2007, 05:48 AM
Thanks for the code Adam, neat and clean. I got a tad bogged down figuring out when and where to call everything, but when I sat down and thought it through there was really no problem at all.

final solution to the datediff problem:

Public Function Wrkdays(vFrm As Form) As Integer

Set dteEnd = vFrm!txtSSD
Set DteStart = vFrm!txtDOR
Dim NonBusDayCounter As Integer, WeekDayConst As Integer
Dim TotalDaysHeld As Integer
Dim x As Double
Dim varBusDays, varFunction



x = 0
NonBusDayCounter = 0

TotalDaysHeld = (dteEnd - DteStart)

Do

varFunction = Weekday(DateAdd("d", x, DteStart))
If varFunction = 1 Or varFunction = 7 Then
NonBusDayCounter = NonBusDayCounter + 1
End If
x = x + 1

Loop Until x = TotalDaysHeld

varBusDays = TotalDaysHeld - NonBusDayCounter

Wrkdays = varBusDays


End Function

Public Function amdValid(vFrm As Form) As Boolean

amdValid = True

If (Wrkdays(vFrm) > 3 And Nz(vFrm!txtComments, "") = "") Then
MsgBox "Turnaround Time is greater than 3 days, please comment."
amdValid = False
End If

If (Nz((vFrm!txtSSD Or vFrm!txtDOR), "") = "" And Nz(vFrm!chkStatus, "") = "") Then
MsgBox "Please include Date of Reciept or Site Submission Date"
amdValid = False
End If

If Nz(vFrm!objRequest, "") = "" Then
MsgBox "Please attach Request Document"
amdValid = False
End If

If Nz(vFrm!cboPurchaseOrder, "") = "" Then
MsgBox "Please Select a PO number. If you need to input a new PO number click the add button to the right of the PO field"
amdValid = False
End If

If Nz(vFrm.txtvalue, "") = "" Then
MsgBox "Please Include a Dollar Value for this Record"
amdValid = False
End If

End Function