Calculated Fields in Access Query

mrmarc

Board Regular
Joined
Feb 3, 2014
Messages
79
This works for me in Calculated Field Expression:
IIf([Grade]="N2",[NumberOfCases]*28,0)

But this does not:

IIf([Grade]="N2",[NumberOfCases]*28+[LoosePounds],0)

Both Fields "NumberOfCases" and "LoosePounds" are integers which can be Null.

Any Ideas?
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
same thing with IIf([Grade]="N2",([NumberOfCases]*28)+[LoosePounds],0) ?

or IIf([Grade]="N2",([NumberOfCases]*28+[LoosePounds]),0)

or IIf([Grade]="N2",(([NumberOfCases]*28)+[LoosePounds]),0)

Probably but I've seen stranger fixes
 
Upvote 0
Create a field testfield: =45 and see if the syntax works with that

also maybe set the format to standard, and change from integer to double.
 
Upvote 0
IIf([Grade]="N2",[NumberOfCases]*28+[LoosePounds],0)

Both Fields "NumberOfCases" and "LoosePounds" are integers which can be Null.
The Nz function is a great tool for potentially Null numeric values (see: MS Access: Nz Function)

So try this, which converts any Null values to zero so the math can be done:
Code:
[COLOR=#333333]IIf([Grade]="N2",Nz([NumberOfCases],0)*28+Nz([LoosePounds],0),0)[/COLOR]
 
Upvote 0
The Nz function is a great tool for potentially Null numeric values (see: MS Access: Nz Function)

So try this, which converts any Null values to zero so the math can be done:
Code:
[COLOR=#333333]IIf([Grade]="N2",Nz([NumberOfCases],0)*28+Nz([LoosePounds],0),0)[/COLOR]

Thanks Joe. But I'm getting an error saying that the expression cannot be used in the expression for a calculated field.

Any more ideas?
 
Upvote 0
Create a field testfield: =45 and see if the syntax works with that

also maybe set the format to standard, and change from integer to double.
This didnt work either. Thanks though. A calculated field seems to have its limitations.
 
Upvote 0
Also guys I made a mistake, maybe this will help. This is a calculated field IN a TABLE not a query. Sorry about that.
 
Upvote 0
Thanks Joe. But I'm getting an error saying that the expression cannot be used in the expression for a calculated field.
I re-created the scenario, and it works for me. Based on that, and your other replies, it makes me believe you may not be doing something right.

You are doing this in a query, right?
How exactly are you entering in the Calculated Expression?
Is this a simple Select Query, or an Action Query or Aggregate (Totals) Query?

Please create the query WITHOUT the calculated expression, returning at least all three key fields (Grade, NumberOfCases, and LoosePounds), switch to SQL View, and copy and paste the SQL code here so we can see what your query looks like.
 
Upvote 0
I re-created the scenario, and it works for me. Based on that, and your other replies, it makes me believe you may not be doing something right.

You are doing this in a query, right?
How exactly are you entering in the Calculated Expression?
Is this a simple Select Query, or an Action Query or Aggregate (Totals) Query?

Please create the query WITHOUT the calculated expression, returning at least all three key fields (Grade, NumberOfCases, and LoosePounds), switch to SQL View, and copy and paste the SQL code here so we can see what your query looks like.


Joe, I'm actually using a calculated field in a table. I'm sorry for the confusion!
 
Upvote 0

Forum statistics

Threads
1,221,889
Messages
6,162,624
Members
451,778
Latest member
ragananthony7911

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top