Solved

Access 2010 - report with subreport - dynamically change sort order with a group

Posted on 2013-02-03
8
1,783 Views
Last Modified: 2013-03-06
I have an Access 2010 report with a subreport.  It lists jobs within division, then gives division totals.

The user selects the sort option (via a form).  Within each division, the user can sort by date, job value, or job number.

The parent report just lists division totals.  It is the subreport that lists the job details.

In the subreport I GROUP on division, to give me division header and footer for totals, etc.

How can I set the sort order programmatically if I use the grouping functionality of reports?
0
Comment
Question by:MonkeyPie
  • 4
  • 2
  • 2
8 Comments
 
LVL 84
ID: 38850582
Grouping always takes precedence over sorting, so your sort order will sort within the group.

You can set the OrderBy property on the subreport in the main report's open event:

Me.YourReport.nameOfYoursubreportControl.Report.OrderBy = "SomeField"
Me.YourReport.nameOfYoursubreportControl.Report.OrderByOn = True

Note you can also change the Field used by the grouping in the report:

http://allenbrowne.com/ser-33.html
0
 

Author Comment

by:MonkeyPie
ID: 38853612
Thank you LSMConsulting.
I tried your suggestion and I get an interesting error.  My main report Open Event has the following code:

Private Sub Report_Open(Cancel As Integer)
    If Forms!frmJobSuccessOptions.fraSummaryOption = 2 Then
        Me.srptJobBidSuccess.SourceObject = ""
    Else
        Me.srptJobBidSuccess.SourceObject = "srptJobBidSuccess"
        '    depending on options chosen in form - set up filters in sub report
        
        If Forms!frmJobSuccessOptions.fraPrintWorkloadOptions = 1 Then         'ALL
            Me.srptJobBidSuccess.LinkChildFields = ""
            Me.srptJobBidSuccess.LinkMasterFields = ""
        ElseIf Forms!frmJobSuccessOptions.fraPrintWorkloadOptions = 2 Then     'selected group
            Me.srptJobBidSuccess.LinkChildFields = "GroupDivision"
            Me.srptJobBidSuccess.LinkMasterFields = "GroupDivision"
        ElseIf Forms!frmJobSuccessOptions.fraPrintWorkloadOptions = 3 Then     'selected  controller
            Me.srptJobBidSuccess.LinkChildFields = "Controller"
            Me.srptJobBidSuccess.LinkMasterFields = "Controller"
        End If
'        sub report sort order determined by option on form
         If Forms!frmJobSuccessOptions.fraOrderBy.Value = 1 Then                'Job date
            Me.srptJobBidSuccess.Report.OrderBy = "GroupDivision, [Date], JobNo,  Controller"
        ElseIf Forms!frmJobSuccessOptions.fraOrderBy.Value = 2 Then             'job number
            Me.srptJobBidSuccess.Report.OrderBy = "GroupDivision, JobNo, [Date], Controller"
        ElseIf Forms!frmJobSuccessOptions.fraOrderBy.Value = 3 Then             'job value
            Me.srptJobBidSuccess.Report.OrderBy = "GroupDivision, JobValue desc, JobNo, [Date], Controller"
        End If
        Me.srptJobBidSuccess.Report.OrderByOn = True
    End If

End Sub

Open in new window

The user has various options to choose from:
1. detail report or summary version
2. for all divisions in company, or selected division, or selected 'controller'
3. sort order by date, or job no, or job value (descending order)

You will see that I don't want the sub report to print if user has selected SUMMARY option.
 Before I added the SORT code this all worked correctly.  But now I get error 2455 "You entered an expression that has an invalid reference to property Form/Report" on the line:
Me.srptJobBidSuccess.Report.OrderBy = "GroupDivision, JobValue desc, JobNo, [Date], Controller"

Open in new window

In the Immediate window I get same error when I try any of the following:

?me!srptJobBidSuccess.Report.Name
?me.srptJobBidSuccess.Report.Filter
?me.srptJobBidSuccess.Report.Name

But this is OK:
?me.srptJobBidSuccess.SourceObject.name
Report.srptJobBidSuccess

What am I doing wrong here?
0
 
LVL 84
ID: 38854388
Further investigation reveals that this may be an unreliable method (at best) to handle the OrderBy in your subreport.

A better solution would be to have the subreport look at an open form (for example the Forms!frmJobSuccessOptions form) to get the sort order. To do that, add a textbox to that form and then use code like this in the Open event of the Subreport:

Me.OrderBy = Forms!frmJobSuccessOptions.txSROrderBy
Me.OrderByOn = True

So assuming i have a textbox on frmJobSuccessoptions named "txSROrderBy", this would work, and would be more reliable.
0
 

Author Comment

by:MonkeyPie
ID: 38857498
Your suggestion to try me.orderby=xxx  didn't work.  I got an error.  

I also tried:
1. Allen Browne's idea of setting me.grouplevel(1).controlsource="JobDate" --> error 2191
2. Then tried to set subreport grouplevel in parent report --> error 2455

So, then I tried another idea.  In the subreport I created a sort level under my DIVISION group level an used an expression for the SORT BY field:
=Choose([Forms]![frmJobSuccessOptions].[fraOrderBy],"JobDate","ProjectNumber","ProjectValue")
as the sort level control source.

This doesn't give an error, but it doesn't sort at all!

I like this last option best of all, but why won't it sort my subreport detail records?
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

Author Comment

by:MonkeyPie
ID: 38857501
Sorry the cycle of these posts is so slow, but we are in different time zones.  (Australia for me!)
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 38857550
I'll post a sample of how I did this some time ago...
0
 
LVL 74

Accepted Solution

by:
Jeffrey Coachman earned 500 total points
ID: 38857805
here is a basic sample of using a form to sort a Report Group and also sort the records.
I am sure LSM can help you with the "sub" report syntax.
Access-EEQ23538556DynamicallyCha.mdb
0
 

Author Closing Comment

by:MonkeyPie
ID: 38959754
I have put this aside for now, but think I am going to got with the option to (via code) open sub report in design view, change sort order and filter, save, and then open properly via parent report.
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now