Change Microsoft Access Report Sorting and Grouping with VBA
Question details
Change an Access report's date grouping based on a user's form selection.

- Product
- Microsoft Access
- Device & OS
- not provided
- Scenario
- A user selects a day, week, month, quarter, or year grouping for a report from a form.
- Observed behavior
- The report groups its date records using the selected period and displays them in the configured sort order.
Identify the report's date field and the form control that will hold the period selection. The example uses rptSales, frmCriteria, cboPeriod, and a date field named SaleDate; replace these names with your own.
Set the report's date group in its Open event
Use this approach when a form selection should determine the date grouping each time the report opens.
The example assumes the date field is the report's first group level and that frmCriteria remains open while the report is opened. The report's existing sort order is configured in the Group, Sort, and Total pane.
Open rptSales in Design View. On the Design tab, select Group & Sort, add a group on SaleDate, and set the desired ascending or descending order. Make this the first group level.
Open frmCriteria in Design View and configure cboPeriod to return one of these text values: Day, Week, Month, Quarter, or Year. Save the form and leave it open.
In the report's property sheet, select the Event tab and set On Open to [Event Procedure]. Click the builder button and add this code to the report module: Private Sub Report_Open(Cancel As Integer) Select Case Nz(Forms!frmCriteria!cboPeriod.Value, "") Case "Day": Me.GroupLevel(0).GroupOn = 6 Case "Week": Me.GroupLevel(0).GroupOn = 5 Case "Month": Me.GroupLevel(0).GroupOn = 4 Case "Quarter": Me.GroupLevel(0).GroupOn = 3 Case "Year": Me.GroupLevel(0).GroupOn = 2 Case Else MsgBox "Select a reporting period on the criteria form." Cancel = True End Select End Sub
Select a period in cboPeriod, then open rptSales in Print Preview. To refresh an already open preview when the selection changes, add this code to cboPeriod's After Update event: Private Sub cboPeriod_AfterUpdate() If CurrentProject.AllReports("rptSales").IsLoaded Then DoCmd.Close acReport, "rptSales" DoCmd.OpenReport "rptSales", acViewPreview End If End Sub
Work with Office Documents in WPS Office
WPS Office is a free, lightweight alternative with a familiar interface for working with Office documents and moving existing files into your workflow. It does not run or modify Microsoft Access reports or their VBA code.

Frequently Asked Questions
Why does the report say I need to select a period?
The report's Open event checks Forms!frmCriteria!cboPeriod. Open frmCriteria and select Day, Week, Month, Quarter, or Year before opening the report.
How do I change the report's ascending or descending order?
Open the report in Design View, select Design > Group & Sort, and change the order for the SaleDate group. The VBA example changes the grouping interval but leaves that order in place.
Why doesn't the preview change when I choose another period?
The Report_Open event runs when the report opens. Close and reopen the preview, or add the provided After Update procedure to cboPeriod so it reopens an already loaded report.
Can WPS Office run this Access VBA report?
No. The report and its VBA require Microsoft Access. WPS Office can be used separately to work with compatible Office documents and spreadsheets.




