Use IF condition with ISNULL - Microsoft Access - ms-access

I am trying to calculate a formula but its not workig or i am doing it wrong (obiviously). The scenario is that i have 10 textboxes and a calculation field. I want access to apply a formula on that calculated field (total) according to the data input in those 10 fields.
e.g if the field 9 and 10 is empty then (total) will be divided by 8. Else it should be divided by 9 or ten respectivly. any help in this regard will be appreciated. thanks in advance
what i am expecting
if sub9 is null then total/8
if sub10 is null then total/9
else total/10

That could be:
Final = Total / (8 + Abs(Not IsNull([Field9])) + Abs(Not IsNull([Field10])))

Related

How to create query with simple formula?

Hey is there any way to create query with simple formula ?
I have a table data with two columns value_one and value_two both are decimal values. I want to select this rows where difference between value_one and value_two is grater then 5. How can i do this?
Can i do something like this ?
SELECT * FROM data WHERE (MAX(value_one, value_two) - MIN(value_one, value_two)) > 5
Example values
value_one, value_two
1,6
9,3
2,3
3,2
so analogical difs are: 5, 6, 1, 1 so the selected row would be only first and second.
Consider an example where smaller number is subtracted with a bigger number:
2 - 5 = -3
So, the result is a difference of two numbers with a negation sign.
Now, consider the reverse scenario, when bigger number is subtracted with the smaller number:
5 - 2 = 3
Pretty simple right.
Basically, the difference of two number remains same, if you just ignore the sign. This is in other words called absolute value of a number.
Now, the question arises how to find the absolute value in MySQL?
Answer to this is the built-in method of MySQL i.e. abs() function which returns an absolute value of a number.
ABS(X):
Returns the absolute value of X.
mysql> SELECT ABS(2);
-> 2
mysql> SELECT ABS(-32);
-> 32
Therefore, without worrying about finding min and max number, we can directly focus on the difference of two numbers and then, retrieving the absolute value of the result. Finally, check if it is greater than 5.
So, the final query becomes:
SELECT *
FROM data
WHERE abs(value_one - value_two) > 5;
You can also do complex operations once the absolute value is calculated like adding or dividing with the third value. Check the code below:
SELECT *
FROM
data
WHERE
(abs(value_one - value_two) / value_three) + value_four > 5;
You can also add multiple conditions using logical operators like AND, OR, NOT to do so. Click here for logical operators.
SELECT *
FROM
data
WHERE
((abs(value_one - value_two) / value_three) + value_four > 5)
AND (value_five != 0);
Here is the link with various functions available in MySQL:
https://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html
No, you would just use a simple where clause:
select *
from data
where abs(value_one - value_two) > 5;

MS Access Calc Field with combined fields

I have been trying to resolve this calc field issue for about 30 mins, it looks like I have the single field conditions correct in the expression such as [points] and [contrib] but the combined ([points]+[contrib]) field is not meeting the requirement that sets the field to the correct member type, so when these are added it returns some other member type as basic. Might I use the between operator with the added fields...? I tried it, but there is some compositional error. So in other words if you got 45 points it sets you to basic only named in the points field, if you have contrib of 45 you are set to basic in the calc field as expected, but if it were 50 + 50, instead it is setting to basic when it should be "better" member label. Otherwise this simple statement should seem to be correct but the computer is not reading it so when adding. It must not be recognizing the combined value for some reason and calc fields do not have a sum() func.
Focus here: (([points]+[Contrib]) >= 45 And ([points]+[Contrib]) < 100),"Basic",
IIf(([points] >=45 And [points]<100) Or ([Contrib] >=45 And [Contrib] <100) Or (([points]+[Contrib]) > = 45 And ([points]+[contrib] < 100),"Basic",
IIf(([points] >=100 And [points] <250) Or ([Contrib] >=100 And [Contrib] <250) Or ((([points]+[Contrib]) >=100) And (([points]+[Contrib])<250)),"Better",
IIf(([points] >=250 And [points]<500) Or ([Contrib] >=250 And [Contrib] <500) Or ((([points]+[Contrib]) >=250) And (([points]+[Contrib])<500)),"Great",
IIf(([points] >=500) Or ([Contrib] >=500) Or (([points]+[Contrib]) >=500),"Best","Non-member"))))
Here is a data sample from an Access 2010 table which includes a calculated field named member_type:
id points Contrib member_type
-- ------ ------- ----------
1 1 1 Non-member
2 50 1 Basic
3 200 1 Better
4 300 1 Great
5 600 1 Best
If that is what you want for your calculated field, here is the expression I used for member_type:
IIf([points]+[Contrib]>=45 And [points]+[Contrib]<100,'Basic',IIf([points]+[Contrib]>=100 And [points]+[Contrib]<250,'Better',IIf([points]+[Contrib]>=250 And [points]+[Contrib]<500,'Great',IIf([points]+[Contrib]>=500,'Best','Non-member'))))
In case I didn't get it exactly correct, here is that same expression formatted so that you can better see where you need changes:
IIf([points]+[Contrib]>=45 And [points]+[Contrib]<100,'Basic',
IIf([points]+[Contrib]>=100 And [points]+[Contrib]<250,'Better',
IIf([points]+[Contrib]>=250 And [points]+[Contrib]<500,'Great',
IIf([points]+[Contrib]>=500,'Best','Non-member'
))))
Note if either points or Contrib is Null, member_type will display "Non-member". If that is not the behavior you want, you will need a more complicated expression. Since a calculated field expression can not use Nz(), you would have to substitute something like IIf([points] Is Null,0,[points]) for every occurrence of [points] and IIf([Contrib] Is Null,0,[Contrib]) for [Contrib]
It would be simpler to prohibit Null for those fields (set their Required property to Yes) and set Default Value to zero.
The BETWEEN operator returns TRUE if the value you are testing is >= or <= the limits you have for BETWEEN.
If you are looking at 50+50 then that total = 100 and you are Between 44 and 100. That would result in an answer of "Basic". Change the range for ([points]+[Contrib]) Between 44 And 100) to be ([points]+[Contrib]) Between 44 And 99)

Access 2013 calculated field too complex

Apparently, calculated field is way to complex. Could anyone suggest a better way to accomplish what I'm trying to do?
Goal: The value entered in the Strength Field determines that value that appears in the calculated field. Thanks in advance for any help!
[Strength] number field
[StrMod] Calculated field
Below is the expression I tried to build to support the [StrMod] calculated field.
IIf([Strength]=1,-5,
IIf([Strength]>=2 And [Strength]<=3,-4,
IIf([Strength]>=4 And [Strength]<=5,-3,
IIf([Strength]>=6 And [Strength]<=7,-2,
IIf([Strength]>=8 And [Strength]<=9,-1,
IIf([Strength]>=10 And [Strength]<=11,0,
IIf([Strength]>=12 And [Strength]<=13,1,
IIf([Strength]>=14 And [Strength]<=15,2,
IIf([Strength]>=16 And [Strength]<=17,3,
IIf([Strength]>=18 And [Strength]<=19,4,
IIf([Strength]>=20 And [Strength]<=21,5,
IIf([Strength]>=22 And [Strength]<=23,6,
IIf([Strength]>=24 And [Strength]<=25,7,
IIf([Strength]>=26 And [Strength]<=27,8,
IIf([Strength]>=28 And [Strength]<=29,9,
IIf([Strength]=30,10,Null)
Here are a few suggestions all using a non-calculated data field type.
UPDATE QUERY w/ LOOKUP TABLE
Replace the calculated field type to regular number field for [StrMod]. Create a Strength lookup table:
StrengthValue | StrengthCategory
1 -5
2 -4
3 -4
4 -3
5 -3
6 -2
... ...
Then use this table to create the below update query to be run in the AfterUpdate and AfterInsert data macros for main table or same events in main table's form.
UPDATE maintableName INNER JOIN StrengthLookUp
ON maintableName.Strength = StrengthLookup.StrengthValue
SET maintableName.StrMod = StrengthLookUp.StrengthCategory
UPDATE QUERY w/o LOOKUP TABLE
Replace the calculated field type to regular number field for [StrMod] and simply use an update query in AfterUpdate and AfterEvent events:
UPDATE maintableName
SET maintableName.StrMod =
IIf([Strength]=1,-5,
IIf([Strength]>=2 And [Strength]<=3,-4,
IIf([Strength]>=4 And [Strength]<=5,-3,
IIf([Strength]>=6 And [Strength]<=7,-2,
IIf([Strength]>=8 And [Strength]<=9,-1,
IIf([Strength]>=10 And [Strength]<=11,0,
IIf([Strength]>=12 And [Strength]<=13,1,
IIf([Strength]>=14 And [Strength]<=15,2,
IIf([Strength]>=16 And [Strength]<=17,3,
IIf([Strength]>=18 And [Strength]<=19,4,
IIf([Strength]>=20 And [Strength]<=21,5,
IIf([Strength]>=22 And [Strength]<=23,6,
IIf([Strength]>=24 And [Strength]<=25,7,
IIf([Strength]>=26 And [Strength]<=27,8,
IIf([Strength]>=28 And [Strength]<=29,9,
IIf([Strength]=30,10,Null))))))))))))))))
VBA LOGIC
Replace the calculated field type to regular number field for [StrMod]. Then, use the SELECT CASE statement in the main table's form's AfterInsert and AfterUpdate events:
SELECT CASE Me.Strength
Case 1
Me.StrMod = -5
Case 2 To 3
Me.StrMod = -4
Case 4 To 5
Me.StrMod = -3
Case 6 To 7
Me.StrMod = -3
Case 8 To 9
Me.StrMod = -1
Case 10 To 11
Me.StrMod = 0
Case 12 To 13
Me.StrMod = 1
...
END SELECT
Strictly my preference, but I never work with calculated fields in case of database compatibility (i.e., MS Access 2007 accdb users) and upsizing scalability with programming languages (PHP, Python, VB ODBC connections) and other RDMS (SQL Server, MySQL).
I can not check but you will get the idea:
iif(strength/2 >= 15, null, -5 + INT(strength/2))
Did you want to simplify or complicate? If you want just to update your table once then you don't need any lookup tables...
update table
set StrMode = iif(strength/2 >= 15, null, -5 + INT(strength/2))
But if records are added to table from time to time you will need to run update as well.

BETWEEN operator working wrong in MySQL

I have a query having BETWEEN operator but it showing wrong results
My query-
SELECT * FROM register WHERE height BETWEEN '1' AND '6'
It also shows the user with height 10, 12 and 16 which is wrong. What is the problem with this query?
I have another query which work fine but is not proper way of using as it make query lengthy
SELECT * FROM register WHERE height > 1 AND height < 12
Give me idea for right way of getting the query as if more condition is added it would be hard for the query to understand and code.
Assuming height is an integer should it not be
SELECT * FROM register WHERE height BETWEEN 1 AND 6
You don't need the single quotes
If:
1. You can't or don't want to change the column type
2. The charachters in the field are only numbers
You can change your query to:
SELECT *
FROM register
WHERE CONVERT(height, UNSIGNED INTEGER) BETWEEN 1 AND 6
See my example at this SQL fiddle.
Check the data type of your column 'Height' it should be a int or float or double and if it is amongs above run the following query
SELECT * FROM register WHERE height BETWEEN 1 AND 6
If the height field is not numeric, you could convert it before making you comparison.
SELECT * FROM register WHERE CONVERT(height, unsigned) BETWEEN 1 AND 6

Microsoft Access - grand total adding multiple fields together

I can't quite figure this out. Microsoft Access 2000, on the report total section I have totals for three columns that are just numbers. These =Sum[(ThisColumn1)], 2, 3, etc and those grand totls all work fine.
I want to have another column that says =Sum([ThisColumn1])+Sum([ThisColumn2]) + Sum([ThisColumn3]) but can't figure those one out. Just get a blank so I am sure there is an error.
Give the 3 Grand Totals meaningful Control Names and then for the Grand Grand Total use:
=[GrandTotal1] + [GrandTotal2] + [GrandTotal3]
Your Grand Total formulas should be something like:
=Sum(Nz([ThisColumn1], 0))
NULL values propagate through an expression which means that if any of your three subtotals are blank, the final total will also be blank. For example:
NULL + 10 = NULL
Access has a built in function that you can use to convert NULL values to zero.
NZ( FieldName, ValueIfNull )
You can use NZ in reports, queries, forms and VBA.
So the example above could read like this:
=NZ([GrandTotal1],0) + NZ([GrandTotal2],0) + NZ([GrandTotal3],0)
http://office.microsoft.com/en-us/access/HA012288901033.aspx
Create a new query, and the sql should look like this:
SELECT SUM(Column1 + Column2 + Column3),
SUM(Column1),
SUM(Column2),
SUM(Column3),
FROM Your_Table;