View Full Version : Grouping by selection.


Henley12
02-20-2008, 08:24 AM
Is it possible to write one query and design one report and have different grouping and group headers based on a choice from a form? For instance, I have an equipment listing report, but based on the grouping level selected on a form (i.e. department, location, building, etc), I would like for the report to reflect that. I know I can do it by creating a different report for each grouping I need, but surely there is a way to do it with code and not have to duplicate the reports. Any help would be greatly appreciated. Thanks.

pbaldy
02-20-2008, 08:32 AM
Sure, you can set that type of thing in the open event of the report:

Me.GroupLevel(0).ControlSource = "CarType"

Henley12
02-20-2008, 09:20 AM
That works great! Now, how do I get the chosen grouping level to print out on the report as if it were set up with a group header?

pbaldy
02-20-2008, 09:33 AM
Not clear what you want. Here's the full code from the sample I gave earlier. The report is set up with a group header. "txtGroup" is a textbox in the group header. Now I'm thinking that's what you're after.

Private Sub Report_Open(Cancel As Integer)
Select Case Forms!frmReports.fraGrouping
Case 1 'group by car type
Me.GroupLevel(0).ControlSource = "CarType"
Me.txtGroup.ControlSource = "CarDesc"
Case 2 'group by company
Me.GroupLevel(0).ControlSource = "Company"
Me.txtGroup.ControlSource = "Company"
Case 3 'no grouping, sort by date/time
Me.GroupLevel(0).ControlSource = "DispDateTime"
Me.txtGroup.ControlSource = "DispDateTime"
Me.GroupHeader0.Visible = False
End Select
End Sub

Henley12
02-20-2008, 09:49 AM
But how can I set a generic group header for the report? If I choose a field, it breaks on that field as well as the one chosen on the form.

pbaldy
02-20-2008, 10:02 AM
I guess I'm not understanding the problem. I set the report up with one group header; it doesn't really matter which field is used. Then the code will change it on the fly, based on the user's selection. Can you post a sample?

Henley12
02-20-2008, 10:06 AM
I think I solved the problem. When I set the report up with a header, for instance, Department, and I chose Group By Assigned To, when the report printed it was grouped by the assigned to person, but within that, it was grouped by department. Instead, I did a group on expression, then chose the frame on the form I am using to run the report. I suppose I should point out that I am using Access 2007, as I believe the grouping is a bit different than previous versions.

pbaldy
02-20-2008, 10:17 AM
Glad you got it sorted. 2007 certainly displays the sorting and grouping differently, but it basically works the same way and the code to affect it didn't change (I've done the same thing in 2007). The numeric argument after GroupLevel determines which level, so this would change the second level down:

Me.GroupLevel(1).ControlSource =