Edit Parameter Field Crystal reports with NOT value? - sql

In Crystal Reports, I want to add a WHERE field <> date to filter out dates that have a NULL value from my database in my report.
I'm using a legacy FoxPro database on the backend which generates an SQL statement from my report, but doesn't appear to have anyway of adding a WHERE clause to the generated statement.
When accessing the FoxPro backend directly, dates with psudo-NULL values have a date of 1899-12-30, but when they are pulled from FoxPro through Crystal they appear as 12/30/99 (which is maybe the same date just displayed in MM/DD/YY format).
I noticed that the report had an existing Parameter Field that prompts the user to filter out the original query down to a specific date range. I tried to add my own in addition to the Parameter Field, but discovered that what I needed with my WHERE field <> date is not an available option since there are only 3 types of Field Parameters mainly:
Discrete
Accept single and discrete values.
Ranged
Accept a lower and upper value in order to select everything in this range.
Discrete and Ranged
A combination of the two above
None of these appear able to filter the results of the query using a WHERE NOT type of clause, is there some other way to do this?

Add this to your record-selection formula:
// remove actual nulls
AND Not(Isnull({table.date_field}))
// remove old dates
AND {table.field} <> date(1899,12,30)
// remove dates not in select parameter value
AND {table.field} IN {#date_parameter}

All I really needed to do was add some criteria to the WHERE clause of the SQL statement, simple enough in an SQL client, but when you're doing this in Crystal Reports v10 it's a bit difficult to find, unless you know what you are looking for...
So what I needed to do was:
Select the field to filter by in the report (in the Details section)
Click the Select Expert button on the Experts toolbar.
In the Select Expert dialog the name of your field should appear in a tab.
Below you can select the WHERE criteria used to filter the records.

Related

MSAccess Query Against Multivalued Field with Bogus Condition

I am trying to create a report in Microsoft Access that allows a user to select a variety of criteria from a form (including the option to not select any options for a specific criteria, resulting in no restriction on that field), which are then used as criteria in the record source on the report. I have been successful in doing so with all but two of my criteria, which are querying against multi-valued fields (The user selects a single option from the combobox, but the table being queried allows for multiple values in the field).
The control source for my combobox ("OSHA Carcinogens") is as follows:
SELECT
TblCarcinogens.ID,
TblCarcinogens.[Carcinogen Name]
FROM TblCarcinogens
UNION
SELECT
Null AS AllChoice,
"(All)" AS Bogus
FROM TblCarcinogens
ORDER BY TblCarcinogens.[ID];
The relevant code from my where clause is as follows:
((IIf([Forms]![FrmRPIFilters]![OSHA Carcinogens] Is Not Null,
[RPIMaster].OSHA Carcinogens].[Value]=
[Forms]![FrmRPIFilters]![OSHA Carcinogens],
[RPIMaster].[OSHA Carcinogens].[Value]=
[RPIMaster].[OSHA Carcinogens].[Value]))
<>False));
This method seems to work for anything other than multi-valued fields, but also seems pretty convoluted. I'm open to suggestions about streamlining the where clause in general as well if there is a better way to do it.
When the query is run with an option selected in the OSHA Carcinogens field on FrmRPIFilters, the result set includes only records which have no value stored in this field.
Does anyone have experience with this type of where clause and know what I'm missing?

SSRS 2008/2012 - ADDING AGGREGATION FIELDS

I'm attempting to add a field to an SSRS report based on 2 other displayed fields.
I'm using a matrix report
Field One is a Count of Account numbers
the Second Field is an Order Amount
My Attempt
New_field=(Sum(Amount))/(Count(Account))
What is the best way to do this in SSRS. Because one cannot have Aggregate functions in SSRS.
A second and related issue is Percent increases. What would be the best way to generate Percent differences in a new column.
Notes:
1. The report is fueled using a SQL Stored Procedure.
2. Graphical Display vs tabular are acceptable
Thanks
You can simply put your formula in query and give it an ALIAS. I've also use CASE statement to catch the error when Count(Account)=0.
SELECT
CASE WHEN Count(Account)=0 THEN (Sum(Amount))/(Count(Account)) END AS New_field
FROM TableName

Date in a short text data type field... Select query trouble

In my Access database, I have a table called customers. In this table I have a column called DateEntered. The data type for the field is short text.
The values in this column are not coherent - they come in several variations:
MM-DD-YYYY,
MMDDYYYY and
MM/DD/YYYY.
There doesn't seem to be any standard set.
My goal is to select all customers from 2012. I tried
select *
from customers
where DateEntered <('%2013') AND >('%2012');
but it comes up blank when I run it.
Can anyone point out what I'm failing to do correctly & more importantly explain why exactly this query doesn't work in Access? From my understanding of SQL (not very advanced) this should work.
Another variant)
select * from customers where RIGHT(DateEntered, 4) = '2012'
If you have control over the database and application code, the best way to handle this is to use an actual Date field instead of text in the table.
One way to handle this would be to add a new field to the table, write a query or two to correctly convert the text values to actual date values, and populate the new field.
At this point, you would then need to hunt down the application code the refers to this field in any way and adjust to treat the field as a date, not text. This includes your insert and update statements, report code, etc.
Finally, as a last step, I would rename the original text field (or remove it altogether) and rename the new date field to the original field name.
Once you fix the problem, querying against the field will be a piece of cake.
Alternatively, if you can't alter the table and source code, you can use the date conversion function CDATE() to convert the text value to an actual date. Note that you may need to guard against non-date entries (NULL or empty string values, as well as other text values that aren't really dates in the first place). The IsDate() function can be your friend here.
If you have the time and patience, fixing the data and code is the better approach to take, but sometimes this isn't always feasible.
Why don't you use LIKE operators (they're appropriate when you have a pattern using % and _):
select * from customers where DateEntered like '%2013' or DateEntered like '%2012'

Calculating the Sum of values in ms-access

I have created a query to calculate the sum of all of the profit values in a table, I tried to output this to a textbox on the main form of my database and I just the error #NAME?.
Has anyone tried this before and are there any major things I am missing?
We would need to see some code/design details to understand why your text box gets that #Name error.
Without those details, I'll just suggest you consider a DSum() expression, instead of a query, to load the text box. And DSum() is kind of like a SELECT query, but returns only a single value instead of a result set.
DSum("YourNumericField", "YourTable")
Examine the DSum online help topic for more details. You might find the optional Criteria parameter useful (like a WHERE clause in a SELECT statement) if you ever want to sum only a subset of rows from your table.
DSum("YourNumericField", "YourTable", "account_status = 'ACTIVE'")
If you have create query to calculate the sum called "querySum". On the form property sheet, make sure selection type you change to FORM, go to Data and select "querySum" as record source.
And then, click text box, go to the property sheet and choice Data > Control Source. So you can choice column from query to the text box.
Otherwise, if you want to use VBA. You can do like this
DSum("NumericField", "YourTable")
Or with condition
DSum("NumericField", "YourTable", "type = 'Payment'")

MS Access / SQL - Applying a time condition to multiple queries at once

I have 300+ queries which fetch information from my database. Right now, there is no time condition specified in the queries, so if I wanted to filter all of them by a certain time period, i.e. Between #07/01/2009# And #08/01/2009#, I would have to manually go in each query and add this condition.
All of my queries are populating data into 4 main reports. What I am trying to do is apply a time filtering criteria like the one above to all of my queries at once, so that I can create a weekly report, as well as the Totals report (which just means there's no time condition).
Is there any easy way to add a single parameter before pulling my report that would filter all of my queries at once, and to pull the Totals report if it the parameter field were blank?
You can use a reference to a form control within the WHERE clause of a query. So, for example, if I have an open form named frmDatePicker which includes a text box control named txtStartdate, I could use that control's value as a WHERE condition.
SELECT *
FROM MyTable
WHERE my_date_field >= Forms!frmDatePicker!txtStartDate;
That approach can work, but I have no idea whether it's an appropriate fit for your situation. You would have to modify all of your queries which include that date condition. That could be a one-time-only change. But if you ever change the form and/or control name, you'd have the revise the queries again. (So try not to do that!)
Edit: If you want to allow the user to leave txtStartDate blank, so as not to filter on that date at all, try a WHERE clause like this:
WHERE
my_date_field >= Forms!frmDatePicker!txtStartDate
Or Forms!frmDatePicker!txtStartDate Is Null;