Wrong week number

Ronald1052

New member
Local time
Today, 10:23
Joined
Jan 22, 2014
Messages
1
Hi all.

I'm grouping data into week numbers but now I've stumbled on to an error I think. I use Windows 8.1 and Access 2007.
When I run this piece of code:

Private Sub basWeek()

Dim datDate As Date

datDate = #12/21/2003#
Do Until (datDate > #1/5/2004#)
Debug.Print datDate & " is in week " & DatePart("ww", datDate, vbMonday, vbFirstFourDays)
datDate = DateAdd("d", 1, datDate)
Loop

End Sub

this is the result:

21-12-2003 is in week 51
22-12-2003 is in week 52
23-12-2003 is in week 52
24-12-2003 is in week 52
25-12-2003 is in week 52
26-12-2003 is in week 52
27-12-2003 is in week 52
28-12-2003 is in week 52
29-12-2003 is in week 53
30-12-2003 is in week 1
31-12-2003 is in week 1
1-01-2004 is in week 1
2-01-2004 is in week 1
3-01-2004 is in week 1
4-01-2004 is in week 1
5-01-2004 is in week 2

Watch 29-12-2003. The week number should be 1.
Is this a known bug? Is a fix available or should I fix it myself?
Thanks in advance.
 
It is a know bug! :mad:

I've found the below code on the Internet, (it works for me after a small change). :)

Code:
Function WeekNrAndYear(InputDate As Long) As String
  'Denne funktion erstatter den indbyggede FORMAT-funktion for ugenumre da
  'Format(En_dato, "ww", vbMonday, vbFirstFourDays), ikke returnere de korrekte uger prøv f.eks. 31-12-2007
  'Her returneres uge nr. 53 - det skal være uge nr. 1
  'Returner funktionen ikke det forventede, prøv da at sætte DateValue omkring datofeltet som vis her i eksemplet
  'Eksempel: WeekNrAndYear(DateValue([MoedtTid]))
  Dim a As Integer, B As Integer, C As Long, D As Integer
  WeekNrAndYear = 0
  If InputDate < 1 Then Exit Function
  a = Weekday(InputDate, 1)
  B = Year(InputDate + ((8 - a) Mod 7) - 3)
  Aar = B
  C = DateSerial(B, 1, 1)
  D = (Weekday(C, 1) + 1) Mod 7
  WeekNrAndYear = Format(CStr(Int((InputDate - C - 3 + D) / 7) + 1), "00") & "-" & CStr(Aar)
End Function
 

Users who are viewing this thread

Back
Top Bottom