Microsoft Access IF statement - ms-access

I was wondering if you can make an if statement in Microsoft Access.
The idea is you have 2 columns. 1st one is called "Quantity" (type is number) and 2nd one is called "In stock" (type Yes/No) and want to make the statement:
"if quantity > 0 then "In stock" should be tick otherwise no tick."
I was trying:
iff([quantity] = 0, [in stock] = "No", [in stock] = "Yes")
also tried this
iff([quantity] = 0, [in stock] = false, [in stock] = true)
Hopefully you get it, can someone help or tell me if its possible to make this?
Thanks in advance

If you are using a version of MS Access which offers the Calculated field type, this task is relatively simple and does not require queries or code.
Simply configure your In Stock field to be a Calculated field, calculated using the expression:
[Quantity] > 0
Set the Result Type to Yes/No to indicate that the result will be that of a Boolean field.
The Format may be set as appropriate for your application (Yes/No I would assume).

all editions of Microsoft Access support calculated values in queries. It is a matter of implementing it correctly. i.e.:
In Stock: iff([quantity] = 0, "No", "Yes")
This is put in and creates the new field: In Stock

Firstly, you can't use the IIf function to change the value of a checkbox or a bound control.
If what you want to do is simply determine which item/product is in stock and which isn't, you don't actually need have any field such as the [In Stock] field to be in the table. A more elegant method is to simply evaluate the Quantity field and use an unbound text box to display Yes or No.
Set the unbound textbox's control source to:
=IIf([Quantity] > 0, "In Stock", "Out of Stock")
A slight alternative is:
=IIf([Quantity] > 0, "Yes", "No")
In the case of the slight alternative above, you change the label or header label of the unbound textbox to "In Stock". This way, the Yes or No that will be displayed for each product will make sense to all users and viewers. Please note that the equal sign (=) is part of what you will type.
If for some reason you need to store the [In Stock] field as as a Yes/No data type in the underlying table and you want to update this value to Yes or No (tick or untick), then you could use a macro. In this macro, you would use the traditional If Then Else statement together with the SetValue action. I can't post screenshots/illustrations at the moment because I am currently using a phone. The statement will be as follows:
If [Quantity] > 0
Then SetValue [In Stock] =Yes
Else: SetValue [In Stock] = No
NOTE: you could also subtitle 1 for Yes and 0 for No.

Related

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.

Microsoft Access - query checkbox based on textbox

I have a field text field containing dates.
I also have a checkbox named "Delivered"
If the text field contains a date, I would like the "Delivered" checkbox value to be "True" / ticked.
If the text field isNull, the checkbox value must be "false" / not ticked
I have tried the following in the query expression builder of my checkbox:
IIf([DateField]="";False;True)
but I keep getting an error about the expression being built incorrectly?
You are trying to store a Calculation/dependent value in a table based on a field in the same table, this is not advisable and should not be carried forward. Calculations should be done when and where required, like display on Forms, Query to export or Reports to show. More info on Calculation field is available here : http://allenbrowne.com/casu-14.html
If you really want to then you can create an UPDATE Query as,
UPDATE
tableName
SET
DeliveredFieldName = IIF(Len(DateFieldName & '') = 0, False, True);

IIF statement with a Yes/No Field

I have a question about using an IIF statement with a Yes/No field. In my case, I am trying to get the statement do one thing if the Yes/No field=Yes and another if it is "No." I realize Access stores the Yes/No field as -1,0 and have attempted to formulate the statement this way.
I did make this statement:
NetDonation: IIf([PickupRequired]-1,[DonationValue]-8.75, 0, [DonationValue])
Unfortunately, this statement does not differentiate between the PickupRequired field being "No" or "Yes" and it subtracts 8.75 from all values regardless if the PickupRequired field = No.
IIf() will recognize Yes/No fields as True or False without your having to specify a numeric value, so the following will work just fine
IIf([PickupRequired], "The value is Yes", "The value is No")
In your particular case I suspect that you want
NetDonation: [DonationValue] - IIf([PickupRequired], 8.75, 0)

Use ComboBox as Query Criteria - Boolean

I'm building a query that I'll use in a form to display a list of employees. On my form I have two comboboxes, one to filter the query by end date and one to filter by status.
The source table for the query has a Boolean field (a Yes/No field) which designates whether the employee is available or not, hence the combo to filter by status. I've run into the issue of how to use non-Boolean combo options but still have the query critera be Boolean.
I know that to use a combobox as a criteria I use this syntax: [Forms]![Form1]![Combo4], but since my combo options are "In Training" and "Available" I don't know how to convert the criteria to Boolean... is this even possible?
Example
If my user selects "In Training" from the combo (which would be equal to False on the source table), my query should use False as the criteria for that field.
After searching Google for an hour without any luck, I'm guessing this may not be possible?
Use an IIf expression to transform the combo's text value to Boolean.
IIf([Forms]![Form1]![Combo4] = "Available", True, False)
Note I assumed you want True when the combo's value is "Available" and False for anything else. If the possibilities are more complex, you could use a Switch expression to assign the correct Boolean for each possible combo value ... or use a lookup table which maps between the two.

SSRS Multi Value Parameter. Check whether "Select All" is selected

I have a multi value parameter in my SSRS Report. I want to find out whether (Select All) is checked in that parameter.
In other words, whether all the values in the parameter are checked or only some values are checked.
Is it possible?
I am able to find out number of selected values through Parameters!Parameter.Count. Is there a way to find out total of items in that parameter?
In case anyone is still having issues doing this, I just coded this easy fix.
=IIF(COUNTROWS("dataset").Equals(Parameters!parameter.Count),"it is equal","this is not equal")
For the specific use-case of showing the selected filter on your report in a textbox, here's the expression that will show "All" if "(Select All)" is selected, otherwise it will show all the selected values as a comma-separated list:
=IIF(
Parameters!YourMultivalueParam.Count = countrows("YourDataset"),
"All",
Join(Parameters!YourMultivalueParam.Label,", ")
)
(split onto multiple lines for readability)
countrows reference: https://technet.microsoft.com/en-us/library/dd255215.aspx
Credit to other answers, just want to extend them for this common scenario.
Your approach sounds good: I would make the options for the parameter come from a dataset.
Then you can use =COUNTROWS("DataSetName") to return the total number of options for your parameter and compare this with Parameters!*Parameter*.Count as you suggest.
I also faced this problem and I solved it this way.
I have one multivalued parameter named "Carrier". Then I have added one parameter "CarrierHidden" which is same as "Carrier" only thing is I made its Visibility as Hidden.
="Carrier=" & Switch(Parameters!CarrierHidden.Count = Parameters!Carrier.Count, "All",
Parameters!Carrier.Count > 1 And Parameters!CarrierHidden.Count > Parameters!Carrier.Count, "Multi",
Parameters!Carrier.Count = 1, Parameters!Carrier.Label(0))
The easy way will be to count the number of the selected parameters and compare them to the dataset
=IIF(Parameters!company_number.Count = CountRows("Dataset1"), True, False)
The problem is if you're trying to pull something for another data set then cross referencing the row count in another dataset won't work. You will have to go with what the previous post states. Create an internal parameter of the exact type and assign the default value to the entire dataset. That way you have the max count of the rows since the hidden parameter.count = rowscount. That way you can use it within another dataset also provided that dataset is AFTER the first one is populated.
According to Microsoft's SSRS help search:
=Parameters!<ParameterName>.Count
Returns the integer value 1. For a single-value parameter, the count is always 1.
I verified this does indeed work, check the integer returned for the built-in parameter count field.
Allow multiple values on a parameter selection. Checking the value of the above field will let you know how many values the user actually chose.
In my situation, I allow multiple values on company number. This gives users the ability to choose one company to report on or several at once. Per client request, if they choose more than one, display data horizontally. If only one company is chosen in the parameter list, show the data vertically and hide the other tablix.
So my visibility show or hide expression looks like this in the one tablix:
=IIF(Parameters!company_number.Count > 1, True, False)
and like this in the other:
=IIF(Parameters!company_number.Count = 1,True,False)