Query named SalesWeek --
SELECT Weekday([SalesDate]) AS x, Format([SalesDate],"mm/dd") AS SalesWeek
FROM Sales
WHERE (((Weekday([SalesDate]))=2));
Total Sales for Week --
SELECT SalesWeek.SalesWeek, Sum(Sales.QTY) AS SumOfQTY
FROM Sales, SalesWeek
WHERE (((DateDiff("d",[SalesDate],[SalesWeek])) Between 1 And 7))
GROUP BY SalesWeek.SalesWeek;
I did a few quick tests, but I think this will do it.
Public Function WeekOfMonth2(dteDate As Date) As Integer
'Count Monday as the first day of the week.
'Days in the month before the first Monday
'are treated as belonging to the last week
'of the previous month.
Dim intWeekDay As Integer, intDay As Integer
Dim i As Integer, intFirstMonday As Integer
intWeekDay = Weekday(dteDate)
intDay = Day(dteDate)
'If the day of the month is before the first Monday
'Then get the week of the last day of the previous month
If intWeekDay <> 2 And intDay < 7 Then
WeekOfMonth2 = WeekOfMonth2(DateSerial(Year(dteDate), Month(dteDate),
0))
Else
'Find the first Monday
For i = 1 To 7
If Weekday(DateSerial(Year(dteDate), Month(dteDate), i)) = 2 Then
intFirstMonday = i
Exit For
End If
Next i
WeekOfMonth2 = ((intDay - intFirstMonday) \ 7) + 1
End If
End Function
--
Wayne Morgan
MS Access MVP
"Lynn" <Ly...@discussions.microsoft.com> wrote in message
news:5A4F78B0-1710-49BA...@microsoft.com...
Public Function WeekOfMonth(dteInputDate As Date) As Integer
Dim intDate As Integer
intDate = Day(dteInputDate)
WeekOfMonth = (intDate \ 7) + 1 + (intDate Mod 7 = 0)
End Function
--
Wayne Morgan
MS Access MVP
"Lynn" <Ly...@discussions.microsoft.com> wrote in message
news:3EB7DD12-8C65-4E87...@microsoft.com...
Here's a more general version:
Public Function WeekOfMonth(dteInputDate As Date, intStartWeekday As
Integer) As Integer
Dim intWeekdayOf1st As Integer
Dim intDaysInWeek1 As Integer
Dim intDaysPastWeek1 As Integer
intWeekdayOf1st = WeekDay(DateSerial(Year(dteInputDate),
Month(dteInputDate), 1))
intDaysInWeek1 = (6 + intStartWeekday - intWeekdayOf1st) Mod 7 + 1
If Day(dteInputDate) <= intDaysInWeek1 Then
WeekOfMonth = 1
Else
intDaysPastWeek1 = Day(dteInputDate) - intDaysInWeek1
WeekOfMonth = 1 + (intDaysPastWeek1 \ 7) + Abs(intDaysPastWeek1 Mod 7
<> 0)
End If
End Function
Note: This function can be shortened slightly by using the Ceiling(X) =
- Int(-X) function posted by Van T. Dinh:
WeekOfMonth = 1 - Int(-intDaysPastWeek1 / 7)
When used in a query, use the weekday numbers (Sunday = 1, ..., Saturday
= 7) directly instead of vbSunday, ..., vbSaturday. It counts the days
during the month before the first starting weekday, if any, as week 1.
Also, see the technique used in:
http://groups.google.com/group/microsoft.public.access/msg/c777af0e1fa201c4
Note: The Ceiling function can similarly simplify the expression used there.
James A. Fortune