?
Solved

Date calculation question

Posted on 2013-01-24
7
Medium Priority
?
336 Views
Last Modified: 2013-01-24
On a form I have a field named "txtAck_Date".  Also on the form I have a field named "txtTotalLTdays" which is a number field.  Then there is a 3rd field on the form named "txtProdDate".

I need the field "txtProdDate" to be a calculated field.  When the user enters a date in "txtAck_Date" then "txtProdDate" has to be "txtAck_Date" MINUS the days in "txtTotalLTdays" BUT NOT INCLUDING WEEKENDS (Saturdays or Sundays).

For example, using February of this year, if the user enters 2/21/13 in "txtAck_Date" I need to have "txtProdDate" back up by 20 days if "txtTotalLtDays" is 20.  Therefore "txtProdDate" would be automatically calculated to be 1/24/13.

I can't figure out what the afterupdate event code would be for "txtAck_Date".
0
Comment
Question by:SteveL13
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 38815327
try this:

txtProdDate =  DateAdd( "w", -20 , txtAck_Date)
0
 

Author Comment

by:SteveL13
ID: 38815417
I'm sorry.  One thing all should know is that "txtTotalLtDays" is also a calculated field.  I used 20 as an example but it could be any number.  

Using:

txtProdDate =  DateAdd( "w", -20 , txtAck_Date)

didn't work though.  When I enter 2/21/13 in the "txtShipDate" field 2/1/13 populated "txtProdDate" and it should be 1/24/13 becuase it has to NOT include weekend days.
0
 

Author Comment

by:SteveL13
ID: 38815637
I tried:

 Me.txtProdDate = DateAdd("w", -Val(Me.txtTotalLTdays), Me.txtAck_Date)

And I still get 2/1/13 when I enter 2/21/13 instead of getting 1/24/13.

??
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 

Author Comment

by:SteveL13
ID: 38815803
I meant to type:

I tried:

 Me.txtProdDate = DateAdd("w", -Val(Me.txtTotalLTdays), Me.txtShipDate)

And I still get 2/1/13 when I enter 2/21/13 instead of getting 1/24/13.

??
0
 
LVL 61

Accepted Solution

by:
mbizup earned 2000 total points
ID: 38815887
Okay -- I think you'll have to use a custom function.

Try placing this in a separate module:

Function DateDiffNoWeekends(dt As Date, intNumDays As Integer) As Date
    Dim I As Integer
    Dim dtNew As Date
    I = 0
    dtNew = dt
    Do Until I = intNumDays
        dtNew = dtNew - 1
        Do Until Weekday(dtNew) <> 7 And Weekday(dtNew) <> 1
            dtNew = dtNew - 1
        Loop
        I = I + 1
    Loop
    DateDiffNoWeekends = dtNew
    
End Function

Open in new window


And call it from your after update event like this:

 Me.txtProdDate = DateDiffNoWeekends( Me.txtShipDate,   Me.txtTotalLTdays)

Open in new window



(Im assuming that you will be passing a positive number in txtTotalLTdays)
0
 

Author Comment

by:SteveL13
ID: 38815985
To mbizup:

A perfect work of art!  Thank you.
0
 
LVL 61

Expert Comment

by:mbizup
ID: 38816015
Thanks, Steve!  Glad that helped out :-)

The next reasonable 'step' in these questions is very often "how do I exclude holidays?".

That block of code is a starting point... and if you needed to exclude holidays, you'd have to create a holiday table and use DLookups from that table or some other method within the loop to skip over dates that are included in your holiday table.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question