Issue with conditional expression in Access - ms-access

Essentially I would like to look at a form text box to determine what criteria to add to a SELECT Query in Access.
In a form I have 2 textboxes one representing the dateStart and one for dateEnd. If dateStart is empty the expression should default the criteria to todays date using Date(), and otherwise take the date range between the 2 text boxes.
The following is my current expression:
IIf(IsNull([Forms]![HomeForm]![dateStart]),Date(),Between [Forms]![HomeForm]![dateStart] And [Forms]![HomeForm]![dateEnd])
For some reason when the dateStart, dateEnd fields have a date I get no results.
Note: By itself the Between expression works with no issue, its only when inside the conditional that the issue occurs.

The BETWEEN AND keywords cannot be conditional. Conditional needs to be on each parameter input. Use IIf() with IS NULL or Nz().
Between Nz([Forms]![HomeForm]![dateStart], Date()) And Nz([Forms]![HomeForm]![dateEnd],Date())
Alternatively, instead of dynamic paramterized query, use VBA to conditionally build filter criteria and apply to form or report.

Related

Group projects by Fiscal Year Date expression based on one field value, if field value is null then group projects based on another field value

I am running a report where the projects are grouped by FY based on Fields!EstSubstantial_Completion.Value. Expression below:
=IIf(Month(Fields!EstSubstantial_Completion.Value)=10,
year(Fields!EstSubstantial_Completion.Value)+1,
IIf(Month(Fields!EstSubstantial_Completion.Value)=11,
year(Fields!EstSubstantial_Completion.Value)+1,
IIf(Month(Fields!EstSubstantial_Completion.Value)=12,
year(Fields!EstSubstantial_Completion.Value)+1,
year(Fields!EstSubstantial_Completion.Value))))
The expression is working, but my supervisor would like the projects to first be grouped into a FY based on another date field Fields!Savings_Report_Date.Value first and then if the field is blank (null) reference the Fields!EstSubtantial_Completion.Value field as the date to determine FY grouping.
I am new to SSRS reports so I am unsure if there is a way to write this type of expression.
Thanks!
The easiest way to do this would be to add a calculated field to your dataset.
Go to the dataset properties, then the "Fields" tab, then add a new field, select calculated when prompted.
Give the field a name such as FYCalc and set the expression to
=IIF(Fields!Savings_Report_Date.Value = Nothing, Fields!EstSubtantial_Completion.Value, Fields!Savings_Report_Date.Value)
Now all you need to do is swap out Fields!EstSubtantial_Completion.Value in your current expression and use Fields!FYCalc.Value instead.
Optional :
When you have nested IIF statements, it's often easier to use the SWITCH function instead. It's much easier to read.
=SWITCH(
Month(Fields!FYCalc.Value)=10, year(Fields!FYCalc.Value)+1,
Month(Fields!FYCalc.Value)=11, year(Fields!FYCalc.Value)+1,
Month(Fields!FYCalc.Value)=12, year(Fields!FYCalc.Value)+1,
True, year(Fields!FYCalc.Value)
)
The final True acts like an Else
You could simplify this further like this
=IIF(
Month(Fields!FYCalc.Value) >= 10 and Month(Fields!FYCalc.Value) <= 12, year(Fields!FYCalc.Value)+1,
, year(Fields!FYCalc.Value)
)

Adding an Expression to a MS Access table

I need to add a "STATUS" field to an Access table... Creating the field is the easy part.
I need to display one of three words based on a date from another field.
So in the "Status" field I need to take the date and if it less that 180 days from the date field have the "Status" field display "CURRENT"
If the date is between 181 days and 365 days I need it to display "SUSPENDED" and over 365 days I need it to display "EXPIRED".
If it is also possible to have the field show color based on the current, suspended, expired output that would be a bonus.
I find using calculated columns in tables can be cumbersome and you are limited to what you can do. I would recommend creating a query on your table instead and add the following expression to a new query field:
Status: IIf(DateDiff("d",[YourDateField], Date())<=180,"CURRENT",IIf(DateDiff("d",[YourDateField], Date())>=181 And DateDiff("d",[YourDateField],Date())<365,"SUSPENDED","EXPIRED"))
Make sure to test this a let me know if the calculation is right for each scenario. I may have it backwards.
As far as formatting the field based on the status, this can be accomplished with a textbox in a form or a report. If you select the "Status" textbox in the form/report's design view and then in the ribbon go to the "Format" tab in the "Form/Report Design Tools" menu, click on "Conditional Formatting". There you can specify rules to the textbox background color based on what the status value is.
Hope this helps!
On the form where you are presenting your data you can create a calculated value in a textbox. You can use Conditional Formatting to change the color (in datasheet view) or VBA (in Form view)
Alternatively if you are using a query to present your data on your form you can add another table with these threshold dates and join to it. Then your dates can be dynamic and not hardcoded into your forms.
Use a Switch() expression in a query to derive "Status".
Here it is formatted with each condition/value pair on a separate line. Similar to a VBA Select Case ... End Select block, Switch() returns the value from the first condition which evaluates as True, and ignores the rest.
Switch
(
DateDiff('d', [YourDateField], Date()) < 181, 'CURRENT',
DateDiff('d', [YourDateField], Date()) BETWEEN 181 AND 365, 'SUSPENDED',
DateDiff('d', [YourDateField], Date()) > 365, 'EXPIRED',
True, Null
)
The last condition catches any YourDateField value (eg Null) which doesn't satisfy one of the first three conditions.

query to filter on specific data or no filter if blank

I have a query which filters records based on dates (start date and end date)selected in a previous form. I want the query to filter the specific date range, or output all records if the fields are left blank.
I am unfamiliar with SQL. is there a way to add an if-then statement?
I can use vba if necessary, but would like to use the Access GUI if it is possible.
If you have a parameter, used in WHERE clause (Criteria in query builder) and you want to show all records if parameter is empty, just add this parameter as new column and OR condition where indicate Is Null or, better add a column with expression Nz([MyParam],"") and in Condition area inORrow add""`. Unfortunately in query builder this construction may be quite complicated if you have few parameters, in SQL it looks much simpler, for instance in your case it will be something like this:
WHERE (MyDate >= [paramDateStart] and MyDate <= [paramDateEnd])
OR (Nz([paramDateStart],"")="" AND Nz([paramDateEnd],"") = "")
Sometimes simpler edit SQL and then switch to Design view
You can use these criteria for StartDate and EndDate respectively to compare them to themselves in case one (or both) of the search fields on the form is empty (Null):
>=Nz([Forms]![YourForm]![FromDate], [StartDate])
<=Nz([Forms]![YourForm]![ToDate], [EndDate])

Date field filter is not working in Row Group level filter

I have added a Filter in Row Group-> Group Properties to perform sum of quantity only for those transactions which have done before a certain date.
Whenever I am selecting the 'Cdate' as Expression field from my dataset1, the type is showing as Date/Time but after saving it when I check,I found it as 'Text'. As a result the filter for 'cDate' is not working during report generation.
Note that, I can't filter the data in dataset side or tablix side as I have to show all the column items. This is a matrix report.
OK this is going to be a bit confusing because your dataset contains a field with the same name as one of the built-in expression functions ("CDate" - Convert to Date).
I sometimes run into these datatype issues when using filters and I find the best way to handle it is to force both the filter field and the filter value to be the same data type.
So in your case try setting the Filter expression to:
=CDate(Fields!CDate.Value)
then select the operator as "<=" and set the value using an expression as well:
=CDate(Parameters!MyParameter.value)
and see if that works.
I understand you so that your date field in the dataset is called CDate, try casting it as date, so instead of selecting it in your filter, type the following into the filter
=CDate(Fields!CDate.Value)

calling a query from a report textbox

I have a report based on a number of different queries. Most of the queries use the value of a row's customerID textbox as a key to extract specific data from other fields.
Here is what I have for the Control Source property of one of the textboxes:
=DLookUp([Level],[qryLevel],[Me].[customerID])
Here is the SQL for qryLevel:
SELECT TOP 1 Level, myDate FROM sometable WHERE custID=Me.customerID ORDER BY myDate DESC
qryLevel works when tested independently, but the DLookUp function does not seem to be working properly because Access gives a dialog box asking for each parameter and then outputs #NAME? in the textbox when no values are input into the dialog boxes.
How can I get each of these textboxes to output its own result from a separate query?
DLookup function arguments must all be strings: http://allenbrowne.com/casu-07.html
So for the first two arguments, just enclose them in double-quotes.
For the last argument, the documentation says it's equivalent to a SQL where clause, without the word 'where'. Actually since you're returning only a single record from your query you probably don't need the last argument at all:
=DLookup("[Level]", "[qryLevel]")
Although, I don't see how qryLevel can work as an independent query since it refers to Me which implies a container object. Better to express as:
SELECT TOP 1 Level, myDate
FROM sometable
WHERE custID = [Forms]![MyForm]![customerID]
ORDER BY myDate DESC
... which will work in any context--inside or outside a form.