How sort records in a subreport based on the selection in a combobox on a form

Posted on 2014-08-01
Last Modified: 2014-08-03
Is there a way to sort the records in the detail section of a subreport based on the selection in a combobox on a form.

There are three choices:  Category, Location , and Room.  Each of this is a field in the query which is the reports recordsource.

I don't know if some code in the onopen event of the report would work?

Question by:SteveL13
    LVL 31

    Accepted Solution

    I would do the sorting (and filtering, if that is needed) in code running from a control that opens the report, such as the Report button on my New Style Main Menu sample database.  Create a saved query using the selected sort, and make that query the record source of the subreport.  Here is a link for the Access Archon article featuring this database:
    and here is a screen shot of the main menu:
    New Style main menu
    LVL 56

    Expert Comment

    by:Jim Dettman (Microsoft MVP/ EE MVE)
    You'll have to do it in the OnOpen of the report.  Report's ignore any sorting in an underlying data source.  Set the OrderBy property, and the OrderOnLoad property to True.

    LVL 74

    Assisted Solution

    by:Jeffrey Coachman
    Something like this worked OK for me...

    Obviously there will be other ways to do this, ...but sometimes I just like to manipulate the sorting in the Report's recordsource
     (if no grouping levels are present)
    LVL 84
    Note too that sorting is impacted by Groups as well - so if you're Grouping by FieldA but tell Access to Sort by FieldB, then your records would show on the form "sorted" for FieldA, and for each of those Groups, the records would then be "sorted" by FieldB.

    Author Comment


    I mistakenly accepted your suggestion before I realized that I could not download your example.  Could you report it for me please?

    LVL 74

    Expert Comment

    by:Jeffrey Coachman
    <I could not download your example. >
    Could not download it, ...or could not open it...?

    In any event here it is again, in access 2003 mdb format
    LVL 74

    Expert Comment

    by:Jeffrey Coachman
    LVL 74

    Expert Comment

    by:Jeffrey Coachman

    Note that I used the recordsource here.

    What the other experts posted about using the OrderBy Property is the more used technique.
    For whatever reason,  I was having trouble with Ordeby in a SubReport, (opened by a form)

    So my old fallback is to just grab the Recordsouce and sort that instead.
    What this meas is that you ever add a sort in the report itself, the recordsouce sort will be ignored (as Jim States)

    Finally note that this may all change if your report includes grouping levels.

    So play around with this and also play around with Ordeby.
    again, I can think of about half a dozen approaches to this...


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
    I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
    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…
    With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

    760 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

    7 Experts available now in Live!

    Get 1:1 Help Now