X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
   Reply to this topicStart new topic
> Print Report Based On Form Sort Order, Access 2010    
post Mar 14 2017, 11:32 AM

Posts: 418
Joined: 26-March 13

Hi, I have a form, frmEmployee and this form has a print button.
Users can sort frmEmployee ascending or descending order by fields and I want to run report the same ascending or descending order.

The print button on the frmEmplyee, I added code like below, but query didn't change.

Private sub cmdPrint_click()
Dim strNewRecord as string
strNewRecord = "SELECT * FROM qryEmployee ORDER BY department"

Me.recordsource = strNewRecord
End sub

The report uses the same qryEmployee.

How can I print preview the same order like frmEmployee?
Go to the top of the page
Jeff B.
post Mar 14 2017, 11:58 AM

UtterAccess VIP
Posts: 9,921
Joined: 30-April 10
From: Pacific NorthWet

First, are you printing a form, or running a report?


Jeff Boyce
Microsoft Access MVP (2002-2015)

Mention of hardware or software is, in no way, an endorsement thereof. The FTC of the USA made this disclaimer necessary/possible.
Go to the top of the page
post Mar 14 2017, 12:11 PM

Posts: 418
Joined: 26-March 13

running a report, rptEmployee.

Go to the top of the page
post Mar 14 2017, 04:16 PM

UA Admin
Posts: 31,654
Joined: 20-June 02
From: Newcastle, WA

Ordering is done within the report, so the sort order of the query that provides those records is not that important. Set it here to sort on the fields you want to sort on within the report.

Attached File  2017_03_14_14_13_33.jpg ( 183.1K )Number of downloads: 0

To do this dynamically, you will have to pass an argument from the form to the report and use that to specify the Order By field in the report.

Go to the top of the page
post Mar 14 2017, 05:15 PM

UtterAccess Editor
Posts: 18,033
Joined: 29-March 05
From: Wisconsin


The key is to set up your report with NO sorting in the Grouping and Sorting dialog box when viewing the report in Design View. Then, in the report's Open event you can check the value of the OpenArgs to see what the sort order is.

For example, if the form's .SortOrder property is "LastName DESC", you'd send that to the report via the OpenArgs argument like this:

    DoCmd.OpenReport "rptTest", acViewPreview, "", "", acNormal, Replace(Me.OrderBy, "[" & Me.Name & "].", "")

You do the Replace() bit to strip off the name of the form. You see, if your form's name is "frmTest", your .OrderBy property will look like this: "[frmTest].[strSSN] DESC, [frmTest].[Zip Code]" and you only need the field names and the optional "DESC" for descending.

Then, in your report's Open event, you just use the text you passed like this:

    Me.OrderBy = OpenArgs

Hope this helps,


(;,;) Li'l Cthulu says: Please talk about what you're trying to do, as well as how you're doing it.
Changing your real table name to "Table1" and your real form name to "Form1" in your posts makes it more difficult to understand what's going on, not easier.
Guidelines for Posting Questions
Go to the top of the page
post Mar 20 2017, 11:18 AM

Posts: 418
Joined: 26-March 13

Thank you so much! I got it.
Go to the top of the page

Custom Search
RSSSearch   Top   Lo-Fi    24th January 2018 - 12:58 AM