473,507 Members | 2,451 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Print Report based on Form Sort Order

8 New Member
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:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command11_Click()
  2.     Dim strWhere As String
  3.     If Me.Dirty Then Me.Dirty = False 'save any edits
  4.     If Me.FilterOn Then strWhere = Me.Filter
  5.     DoCmd.OpenReport "rpt_PtNoBeginsWith", acViewPreview, , strWhere
  6.  
  7.     End Sub
I don't know what else to do and I'm not that experienced with VBA.
Nov 7 '11 #1
12 7501
ADezii
8,834 Recognized Expert Expert
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:
Expand|Select|Wrap|Line Numbers
  1. Dim strWhere As String
  2.  
  3. If Me.Dirty Then Me.Dirty = False
  4.  
  5. If Me.FilterOn Then
  6.   strWhere = Me.Filter
  7.     DoCmd.OpenReport "rptEmployees", acViewDesign, , , acHidden
  8.     Reports![rptEmployees].RecordSource = "SELECT * FROM tblEmployees WHERE " & strWhere & _
  9.                                           "ORDER BY [Last] Desc"
  10.     DoCmd.OpenReport "rptEmployees", acViewPreview, , , acWindowNormal
  11. End If
Nov 7 '11 #2
Stewart Ross
2,545 Recognized Expert Moderator Specialist
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:

Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenReport "rpt_PtNoBeginsWith", acViewPreview, , strWhere
  2. With Reports("rpt_PtNoBeginsWith")
  3.   .OrderBy = "[Your Field Name]"
  4.   .OrderByOn = True
  5. 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:

Expand|Select|Wrap|Line Numbers
  1. If Me.OrderByOn then
  2.   With Reports("rpt_PtNoBeginsWith")
  3.     .OrderBy = Me.OrderBy
  4.     .OrderByOn = True
  5.   End With
  6. End If
-Stewart
Nov 7 '11 #3
MsTGordon
8 New Member
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?
Nov 8 '11 #4
Stewart Ross
2,545 Recognized Expert Moderator Specialist
The additional lines of code follow on immediately after the line calling DoCmd.OpenReport on your form's print button.

-Stewart
Nov 8 '11 #5
MsTGordon
8 New Member
@Stewart Ross
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?
Nov 9 '11 #6
Stewart Ross
2,545 Recognized Expert Moderator Specialist
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.OpenReport line then post back what you see in the messagebox:

Expand|Select|Wrap|Line Numbers
  1. 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
Nov 9 '11 #7
MsTGordon
8 New Member
@Stewart Ross
I get the following message:

Orderby= OrderByOn= False
Nov 10 '11 #8
Stewart Ross
2,545 Recognized Expert Moderator Specialist
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
Nov 10 '11 #9
MsTGordon
8 New Member
@Stewart Ross
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.
Nov 10 '11 #10
Stewart Ross
2,545 Recognized Expert Moderator Specialist
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
Nov 10 '11 #11
MsTGordon
8 New Member
@Stewart Ross
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".
Nov 10 '11 #12
Stewart Ross
2,545 Recognized Expert Moderator Specialist
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:

Expand|Select|Wrap|Line Numbers
  1. [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
Nov 10 '11 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

1
3557
by: Nothing | last post by:
Question: I have a form, open to a record, displaying some information. I have a button, on the form, that I want to click and have a print funtion on it. I want it to print the current record to...
2
7234
by: Rosy | last post by:
I am attempting to use the following code to print a report based on the current record in the form. Users bring up the record with a parameter box and then can make changes to the sub-form on the...
2
3305
by: ChadDiesel | last post by:
Hello, I have a form and subform with their tables linked by a field called Load_ID. I have a button on my subform that prints just the items listed on that particular subform (looking at...
0
2000
by: Kaur | last post by:
Hi, I have created a report based on a cross tab query and would like to Sort the column heading of the report based on the sort order of another field. I have three tables called survey...
2
2516
by: Chris | last post by:
Dear All, I have a subform in datasheet view whose record source is a table (not a query) A user can right click and customise sort order. Now when the form is closed VBA code saves for subform...
1
1973
by: thh108688 | last post by:
Hi All, Is anyone know how to do a print report based on my current record. What i would like to get is once i have entered all the value in the form, when i hit the print button, all the value...
17
2391
imrosie
by: imrosie | last post by:
Hello, I've gone through the tutorials, and searched. Still I can't find a clear solution to my issue. I have a form with a subform that displays a listbox control called 'theOrderID'. I have...
0
1056
by: Goomba | last post by:
I am keeping track of artifacts from a certain place (cache) and certain owners. There are many artifacts to a cache and many artifacts to an owner, but cache and owner are not necessarily related....
1
2113
by: mfaisalwarraich | last post by:
Hi Everybody, I have an external database called Patients.mdb where i made a query called qryAdmissionDetails. now i want to run this query on a report of another database called...
1
1338
by: JohnHo | last post by:
why won't this work? I have a form which has a text box: forms!frmCaseLog!tboCaseID the RecordSource for the form is tblCaseLog with the field I would like to print a report with details...
0
7314
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7030
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7482
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5041
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1540
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.