MS Access VBA Correct Week Number between years - vba

I need help with this function. I have a table where based on the posting date I need to assign which week it falls on.
Our week starts on Saturday, so this workaround that Microsoft published some years ago -- doesn't work.
Public Function WOY(MyDate As Date) As Integer ' Week Of Year
WOY = Format(MyDate, "ww", vbSaturday, vbFirstFourDays)
If WOY > 52 Then
If Format(MyDate + 7, "ww", vbSaturday, vbFirstFourDays) = 2 Then WOY = 1
End If
End Function
Would appreciate any help.
Thanks
Valglad

I don't think there is a unique solution here because part of the definition of Week is that it starts on Monday: https://learn.microsoft.com/en-us/office/troubleshoot/access/functions-return-wrong-week-number
One answer That is easy to convert is to assume that Each Saturday gets the same Week Number as the following Monday. Adapting the working solution from: https://learn.microsoft.com/en-us/office/troubleshoot/access/functions-return-wrong-week-number#workarounds
Public Function WOY(MyDate As Date) As Integer ' Week Of Year
MyDate = DateAdd("d", 2, MyDate) 'assuming Saturday gets the same weeknumber as the following Monday
WOY = Format(MyDate, "ww", vbMonday, vbFirstFourDays)
If WOY > 52 Then
If Format(MyDate + 7, "ww", vbMonday, vbFirstFourDays) = 2 Then WOY = 1 'borrowing Microsofts solution to handle all edge cases
End If
End Function
This solution is conceptually simple but then 12/28/19 starts week 1 of 2020.

If our week would start on Monday -- the code in my original message
would flip from 52 to 1 without any issues
For this year perhaps, but not for any year.
Thus, the answer is - as Mathieu clearly explained: There is no solution to your question.
It is for a reason, that the ISO 8601 standard was created where week numbering is an important part, as the first and last week may span calendar year boundaries.
You may use any other week numbering system, but most likely it will have shortcomings as you have seen.

Related

How to decipher complex DATEADD function from MS Access

I have inherited a query from an old MS Access DB and cannot for the life of me figure out what was trying to be done in this date parameter function. I normally only use SQL and this seems a bit different. Can any one assist in describing what this logic is doing?
use pdx_sap_user
go
select po_number,
po_issue_date
from vw_po_header
where po_issue_date > getDate() And PO_issue_date < DateAdd("d",-1,DateAdd("m",8,DateAdd("d",-(Day(getDate())-1),getDate())))
You can de-obfuscate it a lot by using DateSerial:
where
po_issue_date > getDate() And
po_issue_date < DateSerial(Year(getDate()), Month(getDate()) + 8, 0)
First: there is no getDate() function in Access. Probably it should be Date() which returns the current date.
Now starting from the inner expression:
Day(Date()) returns the current day as an integer 1-31.
So in DateAdd("d", -(Day(Date())-1), Date()) from the current date are subtracted as many days as needed to return the 1st of the current month.
Then:
DateAdd("m", 8, DateAdd("d", -(Day(Date())-1), Date()))
adds 8 months to the the 1st of the current month returning the 1st of the month of the date after 8 months.
Finally:
DateAdd("d", -1,...)
subtracts 1 day from the date returned by the previous expression, returning the last day of the previous month of that date.
So if you run today 13-Sep-2019 this code, the result will be:
30-Apr-2020
because this is the last day of the previous month after 8 months.
I think the following:
Take the current date
Substract the current day of month -1 to get the first day of current month
Add 8 month to this
Substract 1 day to get the last day of the previous month
So it calculates some deadline in approx 8 months.
But I wonder how a PO issue date can be in the future...

VBA convert week and day to date

I have a table with week number and day of week.
Example:
J2 = 18
G2 = Monday
How can I convert this to 2018-04-30?
I can find lots of threads for converting the other way around, meaning date to week. But I can't find anything on week + weekday to date.
Anyone know of a method?
The (optional) second argument of the WEEKDAY function determines what the first day of the week is. The two most common choices are:
1: Sunday (1) to Saturday (7)
or
2: Monday (1) to Sunday (7)
But, you can start on Wednesday, Friday, etc if you want. Pick an option now.
So, start with 1st January (of whichever unspecified year you're working with), and subtract the weekday of 1st January, according to whichever start-day you picked. This gives you a baseline for Week 1. To get to the start of Week n, you just need to add 7*(n-1) days.
Finally, you need to add the weekday back on. I'm going to recommend using MATCH on an array. Your array will be your weekdays, in order, surrounded by curly brackets. So, for Sunday-to-Saturday, you would use
MATCH(G2,{"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"},0)
to get a number from 1 ("Sunday") to 7 ("Saturday"), or #NA if cell G2 does not contain a day-name.
Stick it all together, and you get something like this:
=DATE(YEAR(NOW()),1,1) - WEEKDAY(DATE(YEAR(NOW()),1,1),1) + 7*(J2-1) + MATCH(G2,{"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"},0)
Try this, necessary comments in code:
Option Explicit
Sub GetDate()
Dim year As Date, day As Long
'convert day in G2 to integer
Select Case Range("G2").Value
Case "Monday"
day = 1
Case "Tuesday"
day = 2
'rest of cases
End Select
'declare what year you want
year = "01-01-2018"
'move from first week to the week specified in J2
year = DateAdd("d", (Range("J2").Value - 1) * 7, year)
'move back to first day of the week
Do While DatePart("w", year, vbMonday) <> 1
year = DateAdd("w", -1, year)
Loop
'find date of particular day
Do While DatePart("w", year, vbMonday) <> day
year = DateAdd("w", 1, year)
Loop
End Sub
The determination depends critically on your definition of "week number".
If you are using the ISO-8601 definition, where a Week starts on Monday and week 1 contains the first Thursday of the year, you can use this worksheet formula:
=DATE(YEAR(TODAY()),1,-2)-WEEKDAY(DATE(YEAR(TODAY()),1,7),14)+$J$2*7+MATCH($G$2&"*",{"Monday";"Tuesday";"Wednesday";"Thursday";"Friday";"Saturday";"Sunday"},0)-1
you may need to change the commas to semicolons depending on your regional settings
To explain:
First, find the last Monday of the previous year:
DATE(YEAR(TODAY()),1,-2)-WEEKDAY(DATE(YEAR(TODAY()),1,7),14)
Then add seven(7) times the number of desired weeks:
+$J$2*7
Then add the number of days from Monday to the desired day for that week:
+MATCH($G$2&"*",{"Monday";"Tuesday";"Wednesday";"Thursday";"Friday";"Saturday";"Sunday"},0)-1

Start of the week (Monday) - Query gives an incorrect date

I have this query to get the start of the week (Monday) for the current date. It breaks when the date is first of the month and a Sunday. Not sure how to solve this issue. Any help would be highly appreciated. Thanks.
DATEADD(wk,DATEDIFF(wk,0,'2017-01-01'),0)
This is happening because you are finding the no. of weeks from date 0 to the supplied date. 0 is actually '1900-01-01' which was a Monday. Therefore,DATEDIFF finds the number of completed weeks from this date until the supplied data. Thats why DATEDIFF for March 27 2016 returns 6065 (as it is the end of a week) and March 26 2016 returns 6064 (as it is still not the end of a week).
This is already explained in this link - Get first day of the week
Try the code mentioned below. Is that what you are looking for?. Let me know if this works
declare #date date = '2017-01-01'
select case when dateadd(dd,-datepart(dw,#date)+2,#date) < dateadd(dd,-day(#date)+1,#date)
then #date else dateadd(dd,-datepart(dw,#date)+2,#date) end as firstdayoftheweek

Day360 in vb.net

I want to use the Days360 function in VB.Net. I need to know the difference in days between two dates assuming 360 days in a year (not 365 days the DateDiff function uses).
For example DateDiff(DateInterval.Day,"16/10/2015", "04/02/2016") = 111 days, but Days360 should return 109 days.
Days360 function in Excel calculates the days between two dates using a fictional calendar that has 30 days in each month. This method is used for some financial purposes.
You can write a function to do the same calculation.
[Edit]
Excel supports two versions of the calculation: one common in the US (this is the default) and the other common in Europe (see the documentation of the DAYS360 function for details).
The code I originally posted implemented the European version. I have updated it to support both versions. Thanks to Nikhil Vartak for pointing this out.
Function Days360(startDate As DateTime, endDate As DateTime, euMethod As Boolean) As Integer
Dim months As Integer = (endDate.Year - startDate.Year) * 12 + endDate.Month - startDate.Month
If euMethod Then
'Use European method (start or end dates after the 30th of the month are changed to 30th)
Return months * 30 + Math.Min(30, endDate.Day) - Math.Min(30, startDate.Day)
Else 'Use US method
'If the start date is the last day of the month, change it to the 30th
Dim startDay As Integer = startDate.Day
startDay = If(startDate.Day >= DateTime.DaysInMonth(startDate.Year, startDate.Month), 30, startDate.Day)
'If end date is last of the month, change it to the 30th
Dim endDay As Integer = endDate.Day
endDay = If(endDate.Day >= DateTime.DaysInMonth(endDate.Year, endDate.Month), 30, endDate.Day)
'If end date is last of the month and start date is before 30th, change end date to 1st of the next month
If endDate.Day >= DateTime.DaysInMonth(endDate.Year, endDate.Month) And startDay < 30 Then
endDay = 1
months += 1
End If
Return months * 30 + endDay - startday
End If
End Function

Find first and last day for previous calendar month in SQL Server Reporting Services (VB.Net)

I'm creating a report in MS SQL Server Reporting Services and need to set the default Start and End Date report parameters to be the first and last dates of the previous calendar month and need help.
The report is generated on the 2nd calendar day of the month and I need values for:
Previous Calendar Month
- first day
- last day
I've been working with DateAdd, but have not been successful at creating an Expression (in VB.NET as I understand it). I would really appreciate any help you can give me!
Randall, here are the VB expressions I found to work in SSRS to obtain the first and last days of any month, using the current month as a reference:
First day of last month:
=dateadd("m",-1,dateserial(year(Today),month(Today),1))
First day of this month:
=dateadd("m",0,dateserial(year(Today),month(Today),1))
First day of next month:
=dateadd("m",1,dateserial(year(Today),month(Today),1))
Last day of last month:
=dateadd("m",0,dateserial(year(Today),month(Today),0))
Last day of this month:
=dateadd("m",1,dateserial(year(Today),month(Today),0))
Last day of next month:
=dateadd("m",2,dateserial(year(Today),month(Today),0))
The MSDN documentation for the VisualBasic DateSerial(year,month,day) function explains that the function accepts values outside the expected range for the year, month, and day parameters. This allows you to specify useful date-relative values. For instance, a value of 0 for Day means "the last day of the preceding month". It makes sense: that's the day before day 1 of the current month.
These functions have been very helpful to me - especially in setting up subscription reports; however, I noticed when using the Last Day of Current Month function posted above, it works as long as the proceeding month has the same number of days as the current month. I have worked through and tested these modifications and hope they help other developers in the future:
Date Formulas:
Find the First Day of Previous Month:
DateAdd("m", -1, DateSerial(Year(Today()), Month(Today()), 1))
Find Last Day of Previous Month:
DateSerial(Year(Today()), Month(Today()), 0)
Find First Day of Current Month:
DateSerial(Year(Today()),Month(Today()),1)
Find Last Day of Current Month:
DateSerial(Year(Today()),Month(DateAdd("m", 1, Today())),0)
Dim thisMonth As New DateTime(DateTime.Today.Year, DateTime.Today.Month, 1)
Dim firstDayLastMonth As DateTime
Dim lastDayLastMonth As DateTime
firstDayLastMonth = thisMonth.AddMonths(-1)
lastDayLastMonth = thisMonth.AddDays(-1)
I'm not familiar with SSRS, but you can get the beginning and end of the previous month in VB.Net using the DateTime constructor, like this:
Dim prevMonth As DateTime = yourDate.AddMonths(-1)
Dim prevMonthStart As New DateTime(prevMonth.Year, prevMonth.Month, 1)
Dim prevMonthEnd As New DateTime(prevMonth.Year, prevMonth.Month, DateTime.DaysInMonth(prevMonth.Year, prevMonth.Month))
(yourDate can be any DateTime object, such as DateTime.Today or #12/23/2003#)
in C#:
new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(-1)
new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1)
I was having some difficulty translating actual VB.NET to the Expression subset that SSRS uses. You definitely inspired me though and this is what I came up with.
StartDate
=dateadd("d",0,dateserial(year(dateadd("d",-1,dateserial(year(Today),month(Today),1))),month(dateadd("d",-1,dateserial(year(Today),month(Today),1))),1))
End Date
=dateadd("d",0,dateserial(year(Today),month(Today),1))
I know it's a bit recursive for the StartDate (first day of last month). Is there anything I'm missing here? These are strictly date fields (i.e. no time), but I think this should capture leap year, etc.
How did I do?
I was looking for a simple answer to solve this myself. here is what I found
This will split the year and month, take one month off and get the first day.
firstDayInPreviousMonth = DateSerial(Year(dtmDate), Month(dtmDate) - 1, 1)
Gets the first day of the previous month from the current
lastDayInPreviousMonth = DateSerial(Year(dtmDate), Month(dtmDate), 0)
More details can be found at:
http://msdn.microsoft.com/en-us/library/aa227522%28v=vs.60%29.aspx
This one will give you date no time:
=FormatDateTime(DateAdd("m", -1, DateSerial(Year(Today()), Month(Today()), 1)),
DateFormat.ShortDate)
This one will give you datetime:
=dateadd("m",-1,dateserial(year(Today),month(Today),1))
Dim aDate As DateTime = #3/1/2008# 'sample date
Dim StartDate As DateTime = aDate.AddMonths(-1).AddDays(-(aDate.Day - 1))
Dim EndDate As DateTime = StartDate.AddDays(DateTime.DaysInMonth(StartDate.Year, StartDate.Month) - 1)
'to access just the date portion
' StartDate.Date
' EndDate.Date