Hi Jack,
Will you please clarify further what you specified in "Prior to launching the report reanme.... the "driver seat" by name"? Would appreciate if you can provide sample code on how to do this.
Thanks.
Main Topics
Browse All TopicsI have completed a report with 2 group levels. Now the user requests that the report should allow him to decide on what the group levels should be. In other words, I am required to make the report flexible so that the field on which the report is grouped on is decided by the user.
Can someone help me out on this? I am pretty sure that I will have to write some VB code to do this, but I am not sure on where to start.
My report is currently in the format below:
--------------------------
GroupLevel1
aaa bbb ccc ddd
eee fff ggg hhh
GroupLevel1 total ...
GroupLevel2
xxx yyy zzz
ooo ppp aaa
GroupLevel2 total...
Report total ...
--------------------------
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Query named rptgroupingA
Query named rptgroupingB
Both queries are nearly identical except for the two alias'd column mentioned earlier.
On error resume next
docmd.deleteobject acquery, "rptgroupingINUSE"
On error goto routine_err
DoCmd.Rename "rptgroupingA", acQuery, "rptgroupingINUSE"
docmd.openreport "ReportName",.....
With the report bound the the queryNAME "rptgroupingINUSE" and the groupings set to use the alias instead of the actual field names.
the columns in the report assume the values assigned to the alias.
Build your report with the "dynamic" thing in mind and your users will never know it's the same report.
Thanks Jack.
Now, the second line of the code that you sent me, deletes a query (the query that is currently in use and called rptgroupingINUSE). What scares me about this line is that I am thinking, I will end up with no queries anymore after only 2 runs of the report - both queries would have been deleted by then. This is something I do not want to do. Is there an alternative to 'doCmd.deleteobject' that does a copy instead?
Please confirm if I do have a good case t be concerned before I go ahead with coding.
Thanks.
Good. And yes, I checked out CopyObject and it is the appropriate option to use in this case.
By the way, would there be another alternative to the proposed solution that you have provided - creating 2 queries. Imagine the user requesting (in the future) to have the flexibility to group the report by any one of 4 different fields. I will then have to create 4 queries (nearly identical), which is something I am not really keen to do, if you know what I mean.
If there is an alternative that does not require additional queries, that would be great. However, I may have to stick with your current proposed solution if you can't think of other alternatives to my question.
Thanks.
Public Sub WriteSomeSQL(reportoption as Integer)
dim ssql as string
on error goto WriteSomeSQL_Err
ssql = "SELECT "
select case reportoption
case 1
ssql = ssql & "Field1 as Group1, Field2 as Group2, "
case 2
ssql = ssql & "Field2 as Group1, Field2 as Group2, "
case 3
ssql = ssql & "Field3 as Group1, Field4 as Group2, "
end select
ssql = ssql & "Sum(Field7) as sF7, Sum(Field8) as sF8"
ssql = ssql & vbcrlf
ssql = ssql & "FROM TableName" & vbcrlf
ssql = ssql & "WHERE " & (You could adjust these on the fly as well)
ssql = ssql & vbcrlf
ssql = ssql & "GROUP BY "
case 1
ssql = ssql & "Field1, Field2"
case 2
ssql = ssql & "Field2, Field2"
case 3
ssql = ssql & "Field3, Field4"
end select
ssql = ssql & ";"
currentdb.querydefs("repor
(Exit)
(Error trap)
end sub
Business Accounts
Answer for Membership
by: jadedataPosted on 2003-10-09 at 20:15:44ID: 9525095
Hey! rmission,
Build two nearly identical queries
set two generic column to use for grouping in the report
Assign your grouping in the report to alia group column names
Query Alias columns
Col1: FieldA Col2: FieldB
Col1: FieldB Col2: FieldA
The report references the Col1, Col2 group pattern.
Prior to launching the report rename the queries to put the select group pattern into the "driver seat" by name.
docmd.openreport
and the report appears dynamically grouped
regards
Jack