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 7518 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.OpenRepor t 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.OpenRepor t 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.
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 a pre-defineed report,
which is already built and has the same fields on it as the report, and
then have the report close after printing and return...
|
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 main
form (main form is locked). I use this same code for another
form/report and it works. The only difference is the parameter. The
one that...
|
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 Load_ID). I'm using the following code:
DoCmd.OpenReport "Load Sheet", acPreview, , "= " & "'" &
Me.Parent.Form! & "'"
This works fine, but now I...
|
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 results, survey questions and survey
response choices. Survey response choices has over 800 choices types.
Survey results capture results of all the...
|
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 with the intention that the
sort order is retained. This works fine in an mdb file but with an mde
the form sort order is not saved.
To work...
| |
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 will be printed in a nice word document.
It is good if someone can give me an example when explaining as i am quite new with Access. Sick of coding!...
|
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 set it up (height 4") so that a long list of "saved" orders displays on the Form. The navigation works, so a person can scroll through the list of past...
|
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.
So I have a form called Artifacts. The base form is the Cache form with additional subforms attached to it, including the most important one called...
|
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 PatientDetails.mdb. how i can do this? can this be done by setting the recrodset of a continuous form to the recordset of qryAdmissionDetails and then printing that...
|
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 entered into the form.
For the report RecordSource I have created a query "qryCurrentCaseInfo" which has in the criteria for the control above on...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
| |
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 we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |