Date calculation based on date plus a foem data number

I have a form that has a date1 field.  When this date field is filled in I have another date2 field that I want to populate based on the 1st date field + a field named txtSchedFreqDays.

But there is another field on the form named txtSchedFreqMonths.   In the event this field has a values entered the the formula would be 1st date field + a field named txtSchedFreqMonths.

Either txtSchedFreqDays OR txtSchedFreqMonths will have a number value.  Never both.

But here's the catch... in both cases I never want the date2 field to be populated with a weekend date.  Always move to the following Monday.

Can this be done?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gustav BrockCIOCommented:

Me!txtDate2.Value = DateAdd("d", Nz(Me!txtSchedFreqDays, 0), DateAdd("m", Nz(txtSchedFreqMonths, 0), Me!txtDate1))

To skip a weekend/Holiday, use this function:
Public Function DatePassNonWorkdays( _
    ByVal datDate As Date, _
    Optional ByVal booWorkOnHolidays As Boolean) _
    As Date
'   Returns next workday following datDate.
'   2014-11-03. Cactus Data ApS, CPH
    Dim lngDays     As Long
    Dim lngHoliday  As Long
    Dim booWorkday  As Boolean

        Select Case Weekday(datDate)
            Case vbSaturday
                ' Skip weekend.
                datDate = DateAdd("d", 2, datDate)
            Case vbSunday
                ' Skip Sunday.
                datDate = DateAdd("d", 1, datDate)
            Case Else
                If booWorkOnHolidays = True Then
                    ' Don't check for holidays.
                    booWorkday = True
                    ' Check for holidays to skip.
                    ' Ignore error when using LBound on an unassigned array.
                    On Error Resume Next
                    lngHoliday = LBound(GetHolidays(datDate, datDate))
                    If Err.Number > 0 Then
                        ' Not a holiday.
                        booWorkday = True
                        ' Skip the holiday.
                        datDate = DateAdd("d", 1, datDate)
                    End If
                    On Error GoTo 0
                End If
        End Select
    Loop Until booWorkday = True
    DatePassNonWorkdays = datDate

End Function

Open in new window

Gustav BrockCIOCommented:
Forgot this function to retrieve the Holidays:
Public Function GetHolidays( _
    ByVal datDate1 As Date, _
    ByVal datDate2 As Date, _
    Optional ByVal booDesc As Boolean) _
    As Date()
'   Finds the count of holidays between datDate1 and datDate2.
'   The holidays are returned as an array of dates.
'   DAO objects are declared static to speed up repeated calls with identical date parameters.
'   2014-10-03. Cactus Data ApS, CPH
    ' The table that holds the holidays.
    Const cstrTable             As String = "tblHoliday"
    ' The field of the table that holds the dates of the holidays.
    Const cstrField             As String = "HolidayDate"
    ' Constants for the arrays.
    Const clngDimRecordCount    As Long = 2
    Const clngDimFieldOne       As Long = 0
    Static dbs              As DAO.Database
    Static rst              As DAO.Recordset
    Static datDate1Last     As Date
    Static datDate2Last     As Date
    Dim adatDays()  As Date
    Dim avarDays    As Variant
    Dim strSQL      As String
    Dim strDate1    As String
    Dim strDate2    As String
    Dim strOrder    As String
    Dim lngDays     As Long
    If DateDiff("d", datDate1, datDate1Last) <> 0 Or DateDiff("d", datDate2, datDate2Last) <> 0 Then
        ' datDate1 or datDate2 has changed since the last call.
        strDate1 = Format(datDate1, "\#yyyy\/mm\/dd\#")
        strDate2 = Format(datDate2, "\#yyyy\/mm\/dd\#")
        strOrder = Format(booDesc, "\A\s\c;\D\e\s\c")
        strSQL = "Select " & cstrField & " From " & cstrTable & " " & _
            "Where " & cstrField & " Between " & strDate1 & " And " & strDate2 & " " & _
            "Order By 1 " & strOrder
        Set dbs = CurrentDb
        Set rst = dbs.OpenRecordset(strSQL, dbOpenSnapshot)
        ' Save the current set of date parameters.
        datDate1Last = datDate1
        datDate2Last = datDate2
    End If
    lngDays = rst.RecordCount
    If lngDays = 0 Then
        ' Leave adatDays() as an unassigned array.
        ReDim adatDays(lngDays - 1)
        ' As repeated calls may happen, do a movefirst.
        avarDays = rst.GetRows(lngDays)
        ' rst is now positioned at the last record.
        For lngDays = LBound(avarDays, clngDimRecordCount) To UBound(avarDays, clngDimRecordCount)
            adatDays(lngDays) = avarDays(clngDimFieldOne, lngDays)
    End If
    GetHolidays = adatDays()
End Function

Open in new window

The final expression would look:

Me!txtDate2.Value = DatePassNonWorkdays(DateAdd("d", Nz(Me!txtSchedFreqDays, 0), DateAdd("m", Nz(txtSchedFreqMonths, 0), Me!txtDate1)))

SteveL13Author Commented:
I can't compile on:  DatePassNonWorkdays

I have copy/pasted your 2nd function as a module.

And my final expression now is:  (note that I forgot that the "txtSchedFreqDays" and "txtSchedFreqMonths" are on the main form.  The "txtActualServiceStartDate" and "txtNextScheduleStartDate" are on a subform.

Me!txtNextScheduleStartDate.Value = DatePassNonWorkdays(DateAdd("d", Nz(Me!Forms!frmCustomerSchedule.txtSchedFreqDays, 0), DateAdd("m", Nz(Forms!frmCustomerSchedule.txtSchedFreqMonths, 0), Me!txtActualServiceStartDate)))
Active Protection takes the fight to cryptojacking

While there were several headline-grabbing ransomware attacks during in 2017, another big threat started appearing at the same time that didn’t get the same coverage – illicit cryptomining.

Gustav BrockCIOCommented:
> I can't compile on:  DatePassNonWorkdays

What does "on" mean? The functions both compile.

Maybe it's your expression? For a subform, you must use the name of the subform control, not the name of the form. Thus:

Me!txtNextScheduleStartDate.Value = DatePassNonWorkdays(DateAdd("d", Nz(Me!Forms!NameOfSubformControl.Form!txtSchedFreqDays, 0), DateAdd("m", Nz(Forms!NameOfSubformControl.Form!txtSchedFreqMonths, 0), Me!txtActualServiceStartDate)))

Hamed NasrRetired IT ProfessionalCommented:
Try this as a query: (excluding weekend days), starting on Monday
If a sample database is included, I could have used your field names. Hope that helps.
table a( dd) - dd is your date1 field
form:_tutorial (dd, f1, f2) - f1=txtSchedFreqDays, f2=txtSchedFreqMonths
A query as a record source.
SELECT a.dd AS date1, Val([Forms]![_tutorial]![f1]) AS f11
, Val([forms]![_tutorial]![f2]) AS f22
, DateAdd("d",Val([f11]),DateAdd("m",Val([f22]),[date1])) AS d1
, IIf(Weekday([d1])>5,DateAdd("d",(8-Weekday([d1])),[d1]),[d1]) AS date2

Open in new window

if f1, f2 are in a subform, then use  f11:Forms![_tutorial]!subformControl.Form!f1, and f22:Forms![_tutorial]!subformControl.Form!f2,
SteveL13Author Commented:

Two things...  I did not have the first module in the database so now I have no problem compiling.  But when using this...

Me!txtNextScheduleStartDate.Value = DatePassNonWorkdays(DateAdd("d", Nz(Me!Forms!txtActualServiceStartDate.Form!txtSchedFreqDays, 0), DateAdd("m", Nz(Forms!txtActualServiceStartDate.Form!txtSchedFreqMonths, 0), Me!txtActualServiceStartDate)))

Open in new window

I get an error message..." Microsoft Access can't find the field 'Forms' referred to in your expression."

Gustav BrockCIOCommented:
Sorry, too fast. This is how it should read:

Me!txtNextScheduleStartDate.Value = DatePassNonWorkdays(DateAdd("d", Nz(Me!NameOfSubformControl.Form!txtSchedFreqDays, 0), DateAdd("m", Nz(Forms!NameOfSubformControl.Form!txtSchedFreqMonths, 0), Me!txtActualServiceStartDate)))

I seriously doubt your subform controls (those holding the subforms) are named "txtSomething"

SteveL13Author Commented:
Still not working with...

Me!txtNextScheduleStartDate.Value = DatePassNonWorkdays(DateAdd("d", Nz(Me!txtActualServiceStartDate.Form!txtSchedFreqDays, 0), DateAdd("m", Nz(Forms!txtActualServiceStartDate.Form!txtSchedFreqMonths, 0), Me!txtActualServiceStartDate)))
SteveL13Author Commented:
Error is "Object doesn't support this property or method".
Gustav BrockCIOCommented:
> I seriously doubt your subform controls (those holding the subforms) are named "txtSomething"

I convinced that txtActualServiceStartDate is a textbox, not a subform control.

SteveL13Author Commented:
Am going to send you a stripped down copy.
Gustav BrockCIOCommented:
Please, just obtain the name of the subform control holding the subform (it is not txtActualServiceStartDate ), then correct the expression above.

SteveL13Author Commented:
I am so sorry I don't understand.  When you say "subform control" do you mean the main form name?
Gustav BrockCIOCommented:
No. It is the name of the subform control (on the main form) holding the subform.

SteveL13Author Commented:
Attached because I still don't understand.  

Please open the d/b.  Go to [Customer Schedules] from the Main Menu.

Enter 11/4/2014 in the field named Actual Service Start Date.  

Then the date that populates Next Sched Start Date should be 30 days after Actual Service Start Date but not allow for holidays or weekends.

Again, sorry I'm so confused.
Gustav BrockCIOCommented:
Oh, I see. You are in the subform. Then it is the other way round ... refer to the parent form:

Me!txtNextScheduleStartDate.Value = DatePassNonWorkdays(DateAdd("d", Nz(Me.Parent!txtSchedFreqDays, 0), DateAdd("m", Nz(Me.Parent!txtSchedFreqMonths, 0), Me!ActualServiceStartDate)))


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SteveL13Author Commented:
Amazing!  Thank you so much.
SteveL13Author Commented:
Amazing!  Thank you so much.
Gustav BrockCIOCommented:
You are welcome!

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.