I have done my research on this in Stack Overflow and am aware of offered solutions whereby the advice is that MSAccess reports override queries and the sort order is usually set in the report properties, or by VBA OnLoad, or something similar. I have noticed something weird in my MS Access reports however, and that is that on one report I have created with the following query the report displays everything in perfect order:
SELECT Format([DateOfEnquiry],"yyyy") AS [Year], Count(T_Enquiry.DateOfEnquiry) AS
NumOfEnquiries, T_Enquiry.YearLevel
FROM T_Enquiry
GROUP BY Format([DateOfEnquiry],"yyyy"), T_Enquiry.YearLevel, IIf([YearLevel] Is
Null,0,Val([YearLevel]))
ORDER BY Format([DateOfEnquiry],"yyyy"), IIf([YearLevel] Is Null,0,Val([YearLevel]));
Here I'm particularly concerned with the ordering/sorting of the [YearLevel] field. [YearLevel] is a text lookup field because it not only contains the integers 1-12 but also has the letters 'K' and 'P' in the lookup field. When running the above query it returns the correct order - that is from 'K' then 'P', then from 1-12. I've used this query as the record source for my report and the report lists all items just as the datasheet does when running the query itself. Perfect!
Now take a look at the following query that I use as a record source for another report:
SELECT Format([DateOfEnquiry],"yyyy") AS [Year], Count(T_Enquiry.Outcome) AS
NumOfEnrolments, T_Enquiry.YearLevel
FROM T_Enquiry
WHERE (((T_Enquiry.Outcome)="Enrolled"))
GROUP BY Format([DateOfEnquiry],"yyyy"), T_Enquiry.YearLevel, IIf([YearLevel] Is
Null,0,Val([YearLevel]))
ORDER BY Format([DateOfEnquiry],"yyyy"), IIf([YearLevel] Is Null,0,Val([YearLevel]));
When this query is run, the datasheet is in perfect [YearLevel] order. However the report view is not. The report view puts [YearLevel] 10 first, then 12, then 2. The only difference (apart from the respective fields) between both SQL queries is the WHERE statement in the second query above. Should this make a difference in report view? I don't see how.
Can anybody please suggest a work around? Or point out what I might be missing in the report properties, VBA code, SQL queries...or maybe there might even be a macro that can sort [YearLevel] more easily in the proper order? I look forward to any advice.
Cheers.
New Information
I have done some more testing and determined that in my report design view I have a text box called Txt_TotalEnrol in the report footer which contains the following calculation:
=Sum([NumOfEnrolments])
This is in addition to some other totals. It seems it is this textbox that causes the order of [YearLevel] to be out. I deleted Txt_TotalEnrol and the ordering of [YearLevel] went back to my desired order.
Why does this operation effect the order of [YearLevel] on the report? Any suggestions very much appreciated.
As #parakmiakos said, check first that there is no other constraint in the OrderBy property of the report that may be overriding your bound query.
If your report is shown in a form, also check that the form doesn't have its own RowSource and OrderBy properties set to something that would explain the behaviour.
I would also not use Year as a field name since it's a reserved word and it may cause strange issues that can be hard to debug.
You could also try to wrap your query into another query (I've made small changes):
SELECT P.TheYear,
P.NumOfEnquiries,
P.YearLevel
FROM (SELECT Format([DateOfEnquiry], "yyyy") AS TheYear,
COUNT(T_Enquiry.DateOfEnquiry) AS NumOfEnquiries,
T_Enquiry.YearLevel,
Val(Nz(YearLevel)) AS YearLevelAsNumber
FROM T_Enquiry
GROUP BY Format([DateOfEnquiry], "yyyy"),
T_Enquiry.YearLevel,
IIf([YearLevel] IS NULL, 0, Val([YearLevel]))) AS P
ORDER BY P.TheYear,
YearLevelAsNumber
EDIT: I had forgotten that reports have a really un-intuitive way of settings sortting and grouping rules.
In Design mode, you need to right-click on an empty part of the report an select Sorting and Grouping:
Then a panel will appear that should let you setup your sorting rules:
Problem solved!
I created a simple query to list all the "Enrolled" [Outcomes]:
SELECT Outcomes
FROM T_Enquiry
WHERE T_Enquiry.Outcomes="Enrolled";
and then inserted the following into the Data Property of the Txt_TotalEnrol textbox in the report:
=DCount("Outcome","qry_TotalOutcomeEnrolled")
This had the desired effect of providing a total number and not re-ordering [YearLevel].
I was using ORDER BY over fields with text and the results were not as expected, so I had tried a GROUP BY but it ended up truncating my text fields at 255 characters. What ended up working for me was still using ORDER BY but for the text fields, using a LEFT statement.
So, instead of:
SELECT text1, text2, text3
FROM table1
ORDER BY text1, text2, text3
I instead put:
SELECT LEFT(text1,50000),LEFT(text2,50000),LEFT(text3,50000)
FROM table1
ORDER BY LEFT(text1,50000),LEFT(text2,50000),LEFT(text3,50000)
It's ridiculous, but it worked for this instance.
Related
I've tried to do a report order by date for almost 2 weeks and still no working properly, and I don't know what can I do anymore :(
This sounds like this, I've a report with 5 fields, one of them is invisible, the Last field of the report is called DEVOLUCAO, and I need my report order by that field ascending, that's the problem, I do a query that works properly in database (access), that works properly in preview data of dataset (visual studio 2010), but in run time it doesn't work, I've already modified the sort expressions in tablix, and do it too on each field of report, but all my ideas has gone...
So I'm using this code to select data:
Select idemprestimo, nomeleitor, titulo, saida, devolucao from tbemprestimohistorico order by devolucao asc
So with this query I can make the report, the report is generated but don't order by devolucao asc, and in preview data it works, directly inside the base it works too, but in run time don't...
So, since the thanks for help
Ps* I'm using vb. Net and access and working on windows form
Don't ever use sorting on the recordsource of an Access report. It will be ignored and may even slow down the opening of the report.
Always apply all sorting in the report itself.
Is there an expression that I can use in MS Access ,to highlight Duplicate entries in Reports?
I tried something like Expression is : Count(*)>1 but it doesn't work.
Br,
I suspect that the original query will need to be bulked up with a sub-query that has an ID column and count of ID. The outermost query will then need to also return the ID count.
Within the report you'd then need add another field that would show the linked ID count if it was > 1.
Access reporting (and forms) allows conditional formatting to be used in a similar way to excel.
See Ribbon: Report Design Tools>Formt>ControlFormatting...
It will let you change the format of a control depending on the value it, or another control, contains.
It's a very nice feature and will also let you add bar charts to you list forms to graphically represent the values sorted in a control.
However, the data set will need to have a column that indicates whether the current row has duplicate records. The snippet from you current query that you provided (that I repeat below) will not do this:
...OR (((Object.Key) In (SELECT [Key] FROM [Object] As Tmp GROUP BY [Key] HAVING Count(*)>1 )));
Without seeing the whole query I can't really help much, but you will need to remove the use of IN and make the SELECT statement a subquery of the main SQL Statement. The main query resultset will need to be LEFT JOINED to the sub query using the Key field. Because of the LEFT JOIN you can use "isnull(Key)" in the SELECT clause and isnull(Key) will be true for non-duplicate rows.
You can then refer to thiscolumn in your conditional formatting
I hope this makes some sense.
You
For a quick and dirty way to highlight duplicate data:
Select the object you want to highlight if it's duplicated, and make
the background white (or whatever the colour of your background is).
Create a copy of the object that you want highlighted if it's
a duplicate.
Format the copy so it has a highlight, and/or add extra text
(eg: DUPLICATE)
Put the copy behind the original (so it can't be seen).
On the original object, select "Hide duplicates" in properties.
Ensure "Hide duplicates" is NOT selected on the copy.
So when the duplicate appears, Access will hide it, but then the object you've created that was originally hidden beneath now becomes visible - effectively highlighting the field.
(Unfortunately it will only highlight the field itself, not the entire section.)
What I am doing
I have a query in Access which as SQL view of:
SELECT Projects.*, Projects.MySortField
FROM Projects
ORDER BY Projects.MySortField DESC;
This query works fine and sorts my data correctly. It is also very, very clean to look at and to understand what is happening.
However, when I load it into my form, I have run into an unexpected problem. Prior to adding Projects.MySortField and the associated sorting, I was able to refer to MySortField as follows:
Me.Recordset("MySortField")
However, now that I've added it to the sort criteria, the query returns slightly difference fields as the record associated field is:
Me.Recordset("Projects.MySortField")
I refer to this somewhat often in VBA which is where problems happen.
What I am trying to do and why
I would like my stakeholders to be able to more easily add or modify sort criteria. Unfortunately some of these are also referred to in the code more explicitly, which means if at any time I want to add sort criteria to my list, it will also adopt the Projects.mFieldName syntax in the RecordSet, which means any code referring to that data will break.
This would be nice to avoid, obviously, and if it was possible to modify the query somehow to facilitate my users adding fields and not having to change any code that would be wonderful.
Specific Question
How can I include Projects.* and still have specific fields from that table for sorting but keep all RecordSet fields reflected as just the field name?
The problem you are running into is that by having Projects.* as well as a named field, you are ending up with two output fields of the same name. I'm not sure why you feel you must use "*" instead of just explicitly naming the fields (which is generally considered the best practice) which would allow you to sort without the problem.
If you want to stick with "*" then you need to uncheck the show box in the query grid for your named fields, which will allow you to have the field explicitly enumerated for sorting or use as a criteria.
The SQL view when you do this should look like:
SELECT Projects.*
FROM Projects
ORDER BY Projects.MySortField DESC;
You can use the unchecking of the show box trick for both sorting or for doing criteria when you are using "*".
You can refer to the sort field without the qualifying table name in the SQL. Here is what I am thinking.
SELECT Projects.*
FROM Projects
ORDER BY MySortField DESC;
This
SELECT Projects.*
FROM Projects
ORDER BY Projects.MySortField DESC;
can be done.
But if you insist on adding , Projects.MySortField,then
SELECT Projects.*, Projects.MySortField as whatever
FROM Projects
ORDER BY Projects.MySortField DESC;
Like a lot of developers who are comfortable with SQL syntax I get frustrated when working with Access's query editor. I'm talking about the raw SQL Syntax view, obviously.
One of its many annoying properties is that upon saving it will discard my layout / formatting. When reopening the query all I see is a bunch of unformatted SQL.
However, if my syntax is long and/or complex enough I've noticed that Access will retain my formatting and layout and, oh joy, the query remains clear and readable. I'm looking at an example right now with a page of SQL containing couple of UNIONs all nicely laid out from a few days ago.
At what point does Access flip over to allowing the user to retain his own formatting? Is it length? Complexity? And is there maybe even a trivial structural edit (if trivial structural isn't an oxymoron) I can make to all my queries which will force Access to leave my layout in place?
There are certain things that Access' query editor is not able to display in design mode.
Queries with UNION are the only thing that come to my mind right now, but there are probably more.
In my experience, Access always changes the layout as long as it's able to display the query in design mode.
As soon as you put something in the query that Access can not display in design mode (like UNION), Access leaves your layout and formatting as it is.
I couldn't figure out why Access kept changing my format in a union query (but not for every query or table included).
I simply created another SELECT query based upon the Union query and corrected everything in design view. It's a lot easier.
When I created the SELET query based upon the UNION query, I included tables or queries that I used as lookup tables and had formatted a field to select the second column from a record in a lookup field that the ubion query had anoyingly converted back to the first field in the selected record (usually the ID No of the record).
For example, I might lookup the account name in a record in the cash disbursements table that should display "Office Supplies Exp" but the Union Query converts at least one of the queries or tables I have combined in the union query to the Account Number, the first record in the lookup table, which was originally hidden in the lookup field.
Just to add to Christian's answer, I've done some more testing and find that UNION and DDL queries are left alone by Access.
If we add Pass through queries to that list, then that would match the queries deemed SQL Specific on the menu:
So, those would seem to be the three special cases.
Before saving just type the word union before the ;.
After opening Access next time, remove the word union and start working. When you want to save, first type union again.
We have a requirement to represent the data on SSRS report in a simple manner.
i.e. the sql query for this report will look like this.
Select col1, col2,....col8 from Table where Date between date1 and date2.
So the user will run this report by selecting it from the web application with some parameters (eg: date1 and date2)
But the critical thing which i am facing here is that on the interface user has the option to select the order of the columns to position as shown in the image below.
In the image you will see the customized format has col1,col4,col8,col6,col5,col3,col7,col2.
Please help me how to create an RDL file to organize this kind of requirement. Thank you in advance in understand the correct requirement and producing the result as requested.
Set the expression for the columns to choose among the different fields depending on the selected parameter. For example, the expression for the value of the second cell might be similar to
=IIF(Parameters!MyColumnParameter.Value = "Choice1", Fields!Col2.Value, Fields!Col4.Value)
(Above code not tested, but hopefully close enough to give you the idea.)