Access DB calc using the an IF function

copperwasher

Board Regular
Joined
Mar 1, 2016
Messages
84
Office Version
  1. 365
Platform
  1. Windows
I have a simple designed Access Database, but I have a challenge.



I need to create an expression that can provide an answer based upon the select of Buy or Sell column…

The Expression used in the 'difference column'
[Current Price]-[Price Entry]

If Buy is selected, the calc (in the difference column) should be [Current Price]-[Price Entry]

If Sell is selected, the calc should be [Price Entry]- [Current Price]

But how do I write the expression in the expression builder, so it uses the right calc for a Buy or Sell trade ?



The table should work as below...
Currently the sell trade calc is wrong and working the same as a buy trade



Buy/Sellprice entrycurrent priceDifferencewinning/losing
Buy1.002001.003000.00100Winning
Buy1.002001.00100-0.00100Losing


Buy/Sellprice entrycurrent pricedifferencewinning/losing
Sell1.002001.001000.00100Winning
Sell1.002001.00300-0.00100Losing


Any guidance is greatly appreciated



CW
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
I'm going to assume you want an expression using IIF - If isn't a function in Access. Also will presume that if buy/sell is not one it has to be the other 1 alternative.

VBA Code:
IIF([Buy/Sell]="Buy",[Current Price]-[Price Entry], [Price Entry]-[Current Price])

If you are storing these calculations you should not; best to do so only on forms or in queries. Also should not use special characters in object names ( / ) nor spaces.
 
Upvote 0
Solution
You can try something like this.

Excel Formula:
Difference : Iif([Buy/Sell]='Buy',  [Current Price]-[Price Entry],  [Price Entry]- [Current Price])
 
Upvote 0
I'm going to assume you want an expression using IIF - If isn't a function in Access. Also will presume that if buy/sell is not one it has to be the other 1 alternative.

VBA Code:
IIF([Buy/Sell]="Buy",[Current Price]-[Price Entry], [Price Entry]-[Current Price])

If you are storing these calculations you should not; best to do so only on forms or in queries. Also should not use special characters in object names ( / ) nor spaces.
Hi Mircron,

Sorry, my bad. I thought I was being thorough enough, clearly not...

Yep, I used the IF as there are just two options BUY or SELL, I thought that was a good way to describe the set-up

Storing:
I am not to conversant with Access.
There are a host of Queries (26), Forms (2), Reports (11) for the database.

Should I change or amend any headings, removing any special characters ?
 
Upvote 0
You can try something like this.

Excel Formula:
Difference : Iif([Buy/Sell]='Buy',  [Current Price]-[Price Entry],  [Price Entry]- [Current Price])
Hi Norie,

Thanks for the expression.

Will I also have potential storage issues with your code ?

I am not to conversant with Access. There are a host of Queries (26), Forms (2), Reports (11) for the database.
Should I change or amend any headings, removing any special characters ?

Just apply the code and fully testing...
 
Upvote 0
Should I change or amend any headings, removing any special characters ?
Not in your currently working db but maybe in a copy that you can switch to later if successful. Or just research naming conventions for the next one.
Your post suggests you didn't notice that I presented practically the same expression..
 
Upvote 0

Forum statistics

Threads
1,214,998
Messages
6,122,643
Members
449,093
Latest member
Ahmad123098

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