[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1132
  • Last Modified:

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?
1 Solution
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now