Function GetDateRange(DateFieldName As String, RangeType As Integer) As String
'Last Month
Dim firstofmonth As Date
Dim lastofmonth As Date
firstofmonth = DateSerial(Year(Date), Month(Date), 0)
lastofmonth = DateSerial(Year(firstofmonth), Month(firstofmonth) - 12, 0)
'Quarterly
Dim begMo As Integer
Dim fdoQtr As Date
Dim ldoQtr As Date
Dim fdoPrevQtr As Date
Dim ldoPrevQtr As Date
begMo = (Month(Date) - 1) \ 3 * 3 + 1
fdoQtr = DateSerial(Year(Date), begMo, 1)
ldoQtr = DateAdd("q", 1, fdoQtr)
fdoPrevQtr = DateAdd("q", -1, fdoQtr)
ldoPrevQtr = DateAdd("q", 1, fdoPrevQtr) - 1
'Custom
Dim dtstart As Date
Dim dtend As Date
Select Case RangeType
Case 48 'All Dates
GetDateRange = "1=1"
Case 49 'This Month
GetDateRange = "Year([" & DateFieldName & "]) = " & Year(Date) & " And Month([" & DateFieldName & "]) = " & Month(Date)
Case 50 'Last Month
GetDateRange = "[" & DateFieldName & "] between #" & Format(firstofmonth, "mm/dd/yyyy") & "# AND #" & Format(lastofmonth, "mm/dd/yyyy") & "#"
Case 51 'Last 30 Days
GetDateRange = "[" & DateFieldName & "] Between #" & (Date - 30) & "# And #" & Date & "#"
Case 52 'Last 60 Days
GetDateRange = "[" & DateFieldName & "] Between #" & (Date - 60) & "# And #" & Date & "#"
Case 53 'Last 90 Days
GetDateRange = "[" & DateFieldName & "] Between #" & (Date - 90) & "# And #" & Date & "#"
Case 54 'Last 12 Months
GetDateRange = "[" & DateFieldName & "] between #" & Format(firstofmonth, "mm/dd/yyyy") & "# AND #" & Format(lastofmonth, "mm/dd/yyyy") & "#"
Case 55 'This Quarter
GetDateRange = "[" & DateFieldName & "] between #" & Format(fdoQtr, "mm/dd/yyyy") & "# AND #" & Format(ldoQtr, "mm/dd/yyyy") & "#"
Case 56 'Last Quarter
GetDateRange = "[" & DateFieldName & "] between #" & Format(fdoPrevQtr, "mm/dd/yyyy") & "# AND #" & Format(ldoPrevQtr, "mm/dd/yyyy") & "#"
Case 57 'This Year
GetDateRange = "Year([" & DateFieldName & "]) = " & Year(Date)
Case 58 'Last Year
GetDateRange = "Year([" & DateFieldName & "]) = " & Year(Date) - 1
Case 59 'Custom
DoCmd.OpenForm "popDate", _
WindowMode:=acDialog
If CurrentProject.AllForms!popDate.IsLoaded Then
dtstart = Nz(Forms!popDate![begDate])
dtend = Nz(Forms!popDate![endDate])
DoCmd.Close acForm, "popDate"
End If
GetDateRange = "[" & DateFieldName & "] Between #" & dtstart & "# And #" & dtend & "#"
End Select
End Function