Programmatically Setting Order By Property in a Three Level Report

I  am filtering and sorting a report by form.   The filter is working fine, but the sort has me stumped.  

The report is 3-levels.  1-ENTITY, 2-DOCUMENT, 3-SUBDOC.   The report's recordsource is a single wide query, and I am using Groupings to nest the SUBDOC data under the related DOCUMENT data, and this under the related ENTITY data.

When I set the Order By property of the report at run time using my form, Access ignores it.  It appears that Access doesn't process the Order By property when the report has Sortings and Groupings.  I get an error:  "You tried to execute a query that does not include the specified expression '[DocumentName]' as part of an aggregate expression."   To get this error to go away I added [DocumentName] (and the other sort fields) to the list of Sortings and Groupings.  The error goes away, but Access does not sort on anything other than the preset sortings and groupings.  That won't help because the client needs to support and filter this report every-which-way.  Here is the code behind the form's [Set Sort Order] button click event:

    Dim strSQL As String, intCounter As Integer
    'Build strSQL String
    For intCounter = 1 To 6
        If Me("cboSort" & intCounter) <> "" Then
        strSQL = strSQL & "[" & Me("cboSort" & intCounter) & "]"
        If Me("Chk" & intCounter) = True Then
            strSQL = strSQL & " DESC"
        End If
        strSQL = strSQL & ", "
    End If

    If strSQL <> "" Then
        'Strip Last Comma & Space
        strSQL = left(strSQL, (Len(strSQL) - 2))
        'Set the OrderBy property
        Reports![rpt_Entity Doc Mgmt Detail].OrderBy = strSQL
        Reports![rpt_Entity Doc Mgmt Detail].OrderByOn = True
        Reports![rpt_Entity Doc Mgmt Detail].OrderByOn = False
    End If

As an alternative I have created a main report with two subreports.  That allows me to get rid of any preset sortings and groupings.  Now I can use the Order By property for the main report, but not the sub reports.  I have not had any luck referencing the Order By property of the sub reports through the subreport controls on the main report.  The client needs to sort the data at the subreport level also.

I am getting a bit frustrated at this point.  All the options I can think of will be big time.  That would be a real bummer since the work would be on my dime.

Am I missing a simple solution?  Anybody have some tricks to share?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

When you have a report without sorting/grouping, the actual recordset of the report *is* the record source, to which the report will happily apply any .Filter and .OrderBy.
When you use sorting/grouping, the report will in fact create a number of queries, collecting information from the .ControlSource properties of all controls. This is how the report manages to display Sum(curPament) in a header or footer. When that mechanism is in place, the .OrderBy becomes meaningless.

If you feel adventurous, you can manipulate the sorting/grouping by program, though. I beleive it can be done only at design time, but this might be a solution.

But I must admit that the entire logic puzzles me. How can you have six level ordering and three level grouping? Surely, the six-level ordering must be applied either before the first grouping or after the last.
* Sort Entities any which way, then show related documents and sub-documents
* Withing entities and documents, sort sub-documents any which way (less likely)

One solution that comes to mind is to create a top-level grouping based on a calculated field. This calcualted field can then be included in the record's query by changing its .RecordSource property. Another is to create *six* top level sorting fields (Sort1, Sort2, etc.) which will be replaced in the query by "Country As Sort1, Department as Sort2, Null As Sort3, Null As Sort4, etc."

Hope this helps

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.