Adding an Expression to a MS Access table - ms-access

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.

Related

Recognition of a date as a date in Access Forms filtering

I have an Access data table with the Field "RequiredUntil", which is from the type "Date/Time", and several other columns, and a form, in which I show that table, and where I usually use right mouse click to filter for what I want. The text box for "RequiredUntil" is formatted as "Short Date".
Part of the form:
When I now do a right click on the field "RequiredUntil" in that form, Access properly recognizes that as a date, and offers me several date filters, i.e. Today, "This week", Past,... So I don't use any SQL there, all is done by Access!
When I select "Today" (or Yesterday or Tomorrow), I get a "Data type mismatch in criteria expression" error.
When I select "This week" (or any other time period), the filter is properly set - but as soon as I filter the next column, I get a
Syntax Error in Query expression '((Year([DataTable].[RequiredUntil])=Year(Date())) AND
(DatePart("ww";[DataTable].[RequiredUntil];0)=DatePart("ww",Date(),0))'.
In that case, the date filter is revoked, and only the second filter is applied.
So, I don't do any SQL, RequiredUntil is of the type Date/Time, the text field in the Form is Date/Time, Access recognizes it as Date - what am I missing?
I'm pretty sure, the issue is caused by Null values in your date field.
If removed, you can set the filter, which will reveal as:
(DateSerial(Year([DataTable].[RequiredUntil]), Month([DataTable].[RequiredUntil]), Day([DataTable].[RequiredUntil])) = Date()+1)
which, obviously, will fail, as DateSerial doesn't accept Null values.

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)
)

MS Access Expression That Includes Dynamic Field Names

I have a crosstab query which returns results based on consumer demand for a bunch of material numbers. The material numbers become my field names in the crosstab query, and later the values from those fields are displayed in a form.
In the form, I display the value in a textbox. There are a couple of these textboxes where I need to sum the total of two or more values from these fields. Not a big deal it's a simple expression. For example (in the Control Source property): =[H123457] + [H123456].
This works well UNTIL there is no demand for a particular material number. In this case, the field doesn't show up in the crosstab query and I'm left trying to sum two fields where one doesn't exist.
I've tried IIf(IsError([H123456]), 0, [H123456]), Null expressions, Nz function, etc but cannot figure out how to dynamically solve the #Name issue that ends up populating the text box.
Essentially what I want is for a 0 value for the field that doesn't exist, so I can add it to the value where the field DOES exist - is this possible?
Regards!
June7 provided the answer in the allenbrowne.com link. Essentially, you need to add all of the possible field names to the Column Headings property in the crosstab query property window. Then it's a simple matter of adding an Nz() function to handle null values.
Thanks June7!

Expression show or hide visibilty on SSRS my Report

I want to hide the entire month in every column with column visibility of a table. Is there any expression to hide the column with condition is
if my month column < my paramater (transdate) will show, and also the otherwise. Please help Thank You So Much
=(Count(Parameters!Dataset1_Periode.Value) < IsDate("1/12/year(Parameters!Dataset1_Periode.Value"), True, False)
---------OR----------
=IIf(Fields!SaldoNBVBulanFebruari.Value >= Parameters!Dataset1_Periode.Value,True ,False)
---------OR----------
=Not(Parameters!Dataset1_Periode.Value = IsDate"1,12,year(Parameters!Dataset1_Periode")
If you never want to show data after a certain date, the most efficient approach is to simply filter it out of your dataset based on the parameter and use a matrix to programmatically generate the columns based on your data.

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)