Hi Gator
A simple way to filter on month is to compare two dates formatted with year
and month only:
"Format([FieldInReport], 'yyyymm')=" & Format([FieldInForm], "\'yyyymm\'")
(note carefully all the single and double quotes!)
Another option is to compare Year and Month values:
"Year([FieldInReport])=" & Year([FieldInForm] _
& " and Month([FieldInReport])=" & Month([FieldInForm]
Both these will be quite slow if you have many records to filter because
they have to process every one of the date fields in your report's
recordsource.
If this is a problem, you can use the following tips to create a more
efficient filter string:
FirstDayOfMonth = DateSerial(Year(TheDate), Month(TheDate), 1)
LastDayOfMonth = DateSerial(Year(TheDate), Month(TheDate)+1, 0)
FirstDayOfNextMonth = DateSerial(Year(TheDate), Month(TheDate)+1, 1)
To check for a date being in a given month you can use either:
[DateField] Between FirstDayOfMonth and LastDayOfMonth
or
[DateField]>=FirstDayOfMonth and [DateField]<FirstDayOfNextMonth
The second one is more reliable if it's possible your date field might also
include a time value.
You could put all this together into a handy function:
Public Function SqlMonthFilter(strDateField As String, dt As Date) As String
Const cDateFormat = "\#yyyy-mm-dd\#"
SqlMonthFilter = "(" & strDateField & " >= " _
& Format(DateSerial(YEAR(dt), month(dt), 1), cDateFormat) _
& " and " & strDateField & " < " _
& Format(DateSerial(YEAR(dt), month(dt) + 1, 1), cDateFormat) _
& ")"
End Function
Then use the function directly to create the WHERE condition string for
OpenReport:
DoCmd.OpenReport "ReportName', acPreview, , _
SqlMonthFilter("[FieldInReport]", [FieldInForm])
--
Good Luck
Graham Mandeno [Access MVP]
Auckland, New Zealand