473,404 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

VBA to display last business day

Hi,

Below is my current VBA module. I am needing to have it pull from the last business day and not todays date. I have read several forums, but am still in need of assistance.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command0_Click()
  2. Dim DATE1 As String
  3. Dim PATH As String
  4.  
  5. DATE1 = Format(Date, "yyyymmdd")
  6. PATH = "N:\CFS\Aging_LCD\Access VBA to Import a File\CAL_Aging_" & DATE1 & ".csv"
  7.  
  8. DoCmd.RunSQL "Delete * from Aging"
  9.  
  10. On Error GoTo Bad
  11. DoCmd.TransferText acImportDelim, "CAL_Aging_specification", "Aging", PATH, True
  12.  
  13. Exit Sub
  14.  
  15. Bad:
  16. MsgBox "The File for the date " & DATE1 & " cannot be found in the directory " & PATH & " OR the file is currently opened by another user"
  17. End Sub
Jul 30 '18 #1
2 2154
NeoPa
32,556 Expert Mod 16PB
sathom27:
the last business day
It might help to know what you mean by this.
Jul 30 '18 #2
zmbd
5,501 Expert Mod 4TB
sathom27,
I agree with NeoPa - a bit more information would be nice; however, I am going to go out on a limb here looking at your posted code that say if today was #7/24/2018# = Tuesday you want Monday the #7/23/2018# and similarly if the date was that Monday (#7/23/2018#) then you want the prior Friday, #7/20/2018#

There are a lot of little algorithms to count days etc; however, this may be the more straight forward approach:

1) You need a table with the holidays. I'd do this even for the more static and stable holidays because society changes.

2) I'm going to use the standard USA work week of Monday through Friday
>> I'm going to be using US date formats, #mm/dd/yyyy#

3) Working logically:
+ Explicitly setting the start of the week as Sunday
+ For Tuesday through Saturday you only need to back one day to get the next business day
+ For Monday you need to go back 3 days to get the previous Friday
+ For Sunday you need to go back 2 days to get the previous Friday


Expand|Select|Wrap|Line Numbers
  1. 'So your pseudo-code looks like
  2. Select Case CurrentDate
  3.   Case Sunday
  4.     -2
  5.   Case Monday
  6.     -3
  7.   Case Between(Tuesday through Saturday)
  8.     -1
  9. End Select
We need to get the day of the week for the current date and the function for that is
Weekday(Date,FirstDayOfWeek)
so that we can subtract the correct number of days...

Expand|Select|Wrap|Line Numbers
  1. 'So your pseudo-code looks like
  2.   lngWDay = WeekdDay(CurrentDate,vbSunday)
  3. '>Notice we're going to override the system setting for the first day of the week
  4.  
  5.   Select Case lngWDay
  6.     Case Sunday
  7.        -2
  8.     Case Monday
  9.       -3
  10.     Case Tuesday to Saturday
  11.       -1
  12.   End Select
And finally DateAdd(Interval, Number, Date)

Expand|Select|Wrap|Line Numbers
  1. 'So your pseudo-code looks like
  2.   lngWDay = WeekdDay(CurrentDate,vbSunday)
  3.   Select Case lngWDay
  4.     Case Sunday 
  5.       DateAdd("d", -2 , CurrentDate)
  6.     Case Monday
  7.       DateAdd("d", -3 , CurrentDate)
  8.     Case Tuesday to Saturday
  9.       DateAdd("d", -1 , CurrentDate)
  10.   End Select
There's a little bit of a trick we can do with Weekday(Date,FirstDayOfWeek) where we can set the start of the week to a specific day of the week and then find the prior day of the week for that day (maybe an example here). So for Sunday and Monday we want the previous Friday so we'll set the week start day as Friday... MIND YOU THIS WILL FAIL if you try to get the prior Friday from a date that is a Friday (#7/27/2018# will return the same date not #7/20/2018# using the following method):
Weekday(FridayDate,vbFriday)=1 we need to back that up one day and negate the returned weekday so that we can use DateAdd()

So for a test date:
DateAdd("d", (-1 * (Weekday(#7/23/2018#, vbFriday) - 1)), #7/23/2018#)
= #7/20/2018#

With this trick in mind we can combine the Sunday and Monday into one case (at this point one could use a If..Then clause instead of Select..Case - I prefer the select case because the intent is clearer to anyone reading the code later - if then may be faster)

Expand|Select|Wrap|Line Numbers
  1. 'So your pseudo-code looks like
  2.   lngWDay = WeekdDay(CurrentDate,vbSunday)
  3.   Select Case lngWDay
  4.     Case Sunday, Monday
  5.       DateAdd("d", (-1 * (Weekday(#CurrentDate#, vbFriday) - 1)), #CurrentDate#)
  6.     Case Tuesday to Saturday
  7.       DateAdd("d", -1 , CurrentDate)
  8.   End Select
Finally we need to compare the returned date against the record set of holidays and adjust if needed.

Try your hand at writing the actual code and post back - we'll help you fix any errors.
Jul 31 '18 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Dan Matthews | last post by:
Hey everybody, I have an 2000 Access database that stores job listings from potential employers for a school. I would like to be able to display the date on a webpage the last time the database...
9
by: Paola | last post by:
I'm a new to asp and I'm trying to display the entired last row on the DB. I try the objRecordset.MoveLast but I get the erros saying "Rowset does not support fetching backward." How I can do that?...
1
by: BLUE WATER | last post by:
Does anyone know how I can see a value in my form field text box ? I tried to add a text box, but the record source doesn't list the variable I want to see for each record. The variable is the...
1
by: Keith | last post by:
How can I make a long web page display at the last point of user entry after postback? Thanks, Keith
2
by: sheenaa | last post by:
Hi, I want to display the last recorded record in the database of SQL SERVER 2005. How can i display it with the select query in the grid view... The database contains the...
3
by: remya1000 | last post by:
i'm using ASP with MSAccess as database. i have two buttons and two textbox in my page. when i press my first button (First month) i need to display the current month in one textbox and last one...
1
by: appu29 | last post by:
In my webpage, when i click "Past or Future" button to display "previous year leave summary or future year leave summary" respectively. It gives the error "Can't use an undefined value as a HASH...
0
by: marlberg | last post by:
Platform: Windows2000, WindowsXP, Windows Vista, etc Language: C#, ASP.NET Pre-compiled Libraries: Enterprise Library 3.0 full I have a requirement to implement in and display in C# and...
8
by: whiteyoh | last post by:
Hi, I have a search box which allows you to look up something and display. What i cant figure out is how to amend the script to display, for example, 5 random stories. Any help is appreciate. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.