Full Version: Group Short Date Records Together By Selecting A Month
UtterAccess Discussion Forums > Microsoft® Access > Access Q and A
Groundrush
Hi All,

Any suggestions to how I can filter records by months of the year rather than by the day?

This is the search function method I currently use which does it by the day of the month

The dat/time field is set to short date.


CODE
Private Sub cmdSearch_Click()
'Set the Dimensions of the Module
Dim strSql As String, strOrder As String, strWhere As String
Dim dbNm As Database
Dim qryDef As QueryDef
Set dbNm = CurrentDb()

strSql = "SELECT PPMControlAll.DateReported " & _
"FROM qryPPMControlAll"

strWhere = "WHERE"

If Not IsNull(Me.txtDateReported) Then
strWhere = strWhere & " (qryPPMControlAll.DateReported) Like '*" & Me.txtDateReported & "*'  AND"
End If

'Remove the last AND from the SQL statment
strWhere = Mid(strWhere, 1, Len(strWhere) - 5)
'Pass the SQL to the RowSource of the listbox
Me.lstTasks.RowSource = strSql & " " & strWhere & "" & strOrder
End Sub



I just need to know how to group them all together by using a combo box that list all 12 months of the year & once a month is selected it will group all the relevant records together.

Any help would be greatly appreciated


Thanks
Jeff B.
Recall that if you "group by month", last year's April's records will show with this year's records. You'll need to group by year and month to get this year's April.
Groundrush
QUOTE (Jeff B. @ Apr 9 2011, 01:30 PM) *
Recall that if you "group by month", last year's April's records will show with this year's records. You'll need to group by year and month to get this year's April.



Ok, the row source type of the combo box is set to Value List & has the following dates set in

CODE
January 2011;February 2011;March 2011;April 2011;May 2011;June 2011;July 2011;August 2011;September 2011;October 2011;November 2011;December 2011



Any idea how do I alter the code to group by year & month?


Thanks
Gustav
Use two colums:
201101;January 2011;201102;February 2011;201103;March 2011; .. etc.

Use the value of column 0 with a width of 0, display column 1 for the user.
Now filter in your query on the expression

Format(DateReported, "yyyymm")

/gustav
Groundrush
QUOTE (Gustav @ Apr 9 2011, 02:47 PM) *
Use two colums:
201101;January 2011;201102;February 2011;201103;March 2011; .. etc.

Use the value of column 0 with a width of 0, display column 1 for the user.
Now filter in your query on the expression

Format(DateReported, "yyyymm")

/gustav



Works a treat, thanks :-)
Gustav
You are welcome!

/gustav
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.