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: - 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.
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: - 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
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: - 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: - If Me.OrderByOn then
-
With Reports("rpt_PtNoBeginsWith")
-
.OrderBy = Me.OrderBy
-
.OrderByOn = True
-
End With
-
End If
-Stewart
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?
The additional lines of code follow on immediately after the line calling DoCmd.OpenReport on your form's print button.
-Stewart
@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?
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: - 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
@Stewart Ross
I get the following message:
Orderby= OrderByOn= False
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
@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.
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
@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".
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: - [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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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....
|
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...
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |