Print Report based on Form Sort Order

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MsTGordon
    New Member
    • Nov 2011
    • 8

    Print Report based on Form Sort Order

    I have a form based on a query where the user can filter and sort in any of the fields of the form. I am able to print preview a report once the user filters the report but if the user sorts the report it does not sort on the report.
    I have a button on my form with the following code on a print button which opens the report:

    Code:
    Private Sub Command11_Click()
        Dim strWhere As String
        If Me.Dirty Then Me.Dirty = False 'save any edits
        If Me.FilterOn Then strWhere = Me.Filter
        DoCmd.OpenReport "rpt_PtNoBeginsWith", acViewPreview, , strWhere
       
        End Sub
    I don't know what else to do and I'm not that experienced with VBA.
    Last edited by Stewart Ross; Nov 7 '11, 11:13 PM. Reason: code tagged
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    The specific Field Sort on the Form is not part of the Form's Filter, and as such, will not reflect on the Report when it Opens. If you know what Fields may be Sorted you can Open the Report in Design/Hidden, then modify the RecordSource Property to reflect the Form's Filter as well as any Sorting, as in:
    Code:
    Dim strWhere As String
    
    If Me.Dirty Then Me.Dirty = False
    
    If Me.FilterOn Then
      strWhere = Me.Filter
        DoCmd.OpenReport "rptEmployees", acViewDesign, , , acHidden
        Reports![rptEmployees].RecordSource = "SELECT * FROM tblEmployees WHERE " & strWhere & _
                                              "ORDER BY [Last] Desc"
        DoCmd.OpenReport "rptEmployees", acViewPreview, , , acWindowNormal
    End If

    Comment

    • Stewart Ross
      Recognized Expert Moderator Specialist
      • Feb 2008
      • 2545

      #3
      If your user form can tell you which field you want to sort on there is a way to do so you may find easier to apply than building a new SQL statement recordsource method, although it is not as flexible. It uses the OrderBy property of the report, like this:

      Code:
      DoCmd.OpenReport "rpt_PtNoBeginsWith", acViewPreview, , strWhere
      With Reports("rpt_PtNoBeginsWith")
        .OrderBy = "[Your Field Name]"
        .OrderByOn = True
      End With
      If you have manually applied sorting to fields displayed on the form, and these have the same name as those used in the report, you can get the field name to sort the report on by referring to the same OrderBy property of the form itself:

      Code:
      If Me.OrderByOn then
        With Reports("rpt_PtNoBeginsWith")
          .OrderBy = Me.OrderBy
          .OrderByOn = True
        End With
      End If
      -Stewart

      Comment

      • MsTGordon
        New Member
        • Nov 2011
        • 8

        #4
        Thank you Stewart for your response. Your second solution seems to be the one that may work for me considering the fields are manually sorted and the same on the report. Where should I put the code you listed? On the print command button or on the "on open" event of the report?

        Comment

        • Stewart Ross
          Recognized Expert Moderator Specialist
          • Feb 2008
          • 2545

          #5
          The additional lines of code follow on immediately after the line calling DoCmd.OpenRepor t on your form's print button.

          -Stewart

          Comment

          • MsTGordon
            New Member
            • Nov 2011
            • 8

            #6
            Originally posted by Stewart Ross
            The additional lines of code follow on immediately after the line calling DoCmd.OpenRepor t on your form's print button.

            -Stewart
            I almost there. I posted the code exactly where you told me on the print button on the form. However when the report opens in Print Preview it is still not sorted like the form.

            Should I have a code on the "On Open" event of my report?

            Comment

            • Stewart Ross
              Recognized Expert Moderator Specialist
              • Feb 2008
              • 2545

              #7
              Should be no need to use the On Open event of your form. Could you check what is being ordered? One way to do so is to add a line of code which will list the value of the OrderBy property. Please add the following line just before your DoCmd.OpenRepor t line then post back what you see in the messagebox:

              Code:
              msgbox "Orderby = " & Me.OrderBy & " OrderByOn = " & Me.OrderByOn
              If OrderBy lists a particular field then the same field with the same name must exists in the underlying recordsource for the report if we are to use the solution I suggested. If not there may well be an alternative way to determine what field to use instead.

              -Stewart

              Comment

              • MsTGordon
                New Member
                • Nov 2011
                • 8

                #8
                Originally posted by Stewart Ross
                Should be no need to use the On Open event of your form. Could you check what is being ordered? One way to do so is to add a line of code which will list the value of the OrderBy property. Please add the following line just before your DoCmd.OpenRepor t line then post back what you see in the messagebox:

                Code:
                msgbox "Orderby = " & Me.OrderBy & " OrderByOn = " & Me.OrderByOn
                If OrderBy lists a particular field then the same field with the same name must exists in the underlying recordsource for the report if we are to use the solution I suggested. If not there may well be an alternative way to determine what field to use instead.

                -Stewart
                I get the following message:

                Orderby= OrderByOn= False

                Comment

                • Stewart Ross
                  Recognized Expert Moderator Specialist
                  • Feb 2008
                  • 2545

                  #9
                  Please advise how you are changing the sort order of the form under user control.

                  From the absence of any SortBy content and the SortByOn property being False when you tested as asked you are clearly not applying a sort to the controls on the form using the menus or a right-click shortcut action. Because there is no OrderBy applying to your form, attempting to apply this property to your report cannot work.

                  If you know which field the form is being sorted on you can apply that knowledge to the OrderBy property of the report, as shown in post #3 above.

                  -Stewart
                  Last edited by Stewart Ross; Nov 10 '11, 06:52 PM.

                  Comment

                  • MsTGordon
                    New Member
                    • Nov 2011
                    • 8

                    #10
                    Originally posted by Stewart Ross
                    Please advise how you are changing the sort order of the form under user control.

                    From the absence of any SortBy content and the SortByOn property being False when you tested as asked you are clearly not applying a sort to the controls on the form using the menus or a right-click shortcut action. Because there is no OrderBy applying to your form, attempting to apply this property to your report cannot work.

                    If you know which field the form is being sorted on you can apply that knowledge to the OrderBy property of the report, as shown in post #3 above.

                    -Stewart
                    I'm sorry this is so confusing. My form is based on a qry which is set to sort by Part Number then Manufacturer by default. The user can then click in any one of the 8 fields on the form to sort and filter should they desire. The print button is a command button on the form which opens a report built on the same qry as the form.
                    If the user filters the form and hits the print button, the report reproduces the form. The user wants to be able to do the same thing if they sort.

                    Comment

                    • Stewart Ross
                      Recognized Expert Moderator Specialist
                      • Feb 2008
                      • 2545

                      #11
                      Please post the on-click code involved in your user sorting. It may well be that a custom SQL recordsource is built and used, like ADezii's solution in post #2. If that is the case you may be better to use ADezii's solution than mine, which was intended for a really simple approach as you had advised you knew little of VBA programming.

                      I have reset the best answer post at present as it may well not be mine which is!

                      -Stewart

                      Comment

                      • MsTGordon
                        New Member
                        • Nov 2011
                        • 8

                        #12
                        Originally posted by Stewart Ross
                        Please post the on-click code involved in your user sorting. It may well be that a custom SQL recordsource is built and used, like ADezii's solution in post #2. If that is the case you may be better to use ADezii's solution than mine, which was intended for a really simple approach as you had advised you knew little of VBA programming.

                        I have reset the best answer post at present as it may well not be mine which is!

                        -Stewart
                        I appreciate your efforts. I am not using a code for the user to sort but the standard MS Access sort buttons. I've arranged custom tool bars to allow the stadard "A-Z" button and "Z-A".

                        Comment

                        • Stewart Ross
                          Recognized Expert Moderator Specialist
                          • Feb 2008
                          • 2545

                          #13
                          Can I just check that when you tried the Msgbox to report the status of OrderBy the form was custom-ordered at the time?

                          I have tried using the toolbar sort facilities on forms in the Northwind sample database, and when I do so the OrderBy property reflects the field on which the sort is taking place, as I would expect it to.

                          Here, for instance, is what the OrderBy property shows for the Northwind Products form after two custom sorts are tasked from the toolbar:

                          Code:
                          [Lookup_SupplierID].[CompanyName] DESC, [Products].[ProductName]

                          If all else fails, you could upload to a new post a version of the database as a ZIP file (removing any confidential information first of course) so we can get the chance to see exactly what is happening with your current database.

                          -Stewart
                          Last edited by Stewart Ross; Nov 10 '11, 09:08 PM.

                          Comment

                          Working...