Too Many Characters in Query

Littlemalky

Board Regular
Joined
Jan 14, 2011
Messages
223
I have this really long formula, but just realized that I need to set negative values to zero. If I put an "IIF Statement" around it, Access tells me there are too many characters. Does anybody have any suggestions?

IIf((IIf(([PAYOUT_DATE]>=[GUARANTEE_START_DATE] And [PAYOUT_DATE]<=[GUARANTEE_END_DATE]),[GUARANTEE_AMOUNT],0))*(IIf(IIf(Day([PAYOUT_DATE])>=Day([GUARANTEE_START_DATE]),0,-1)+(Year([PAYOUT_DATE])-Year([GUARANTEE_START_DATE]))*12+(Month([PAYOUT_DATE])-Month([GUARANTEE_START_DATE]))<4,1,0))=0,[PAYOUT_AMT],(IIf(([PAYOUT_DATE]>=[GUARANTEE_START_DATE] And [PAYOUT_DATE]<=[GUARANTEE_END_DATE]),[GUARANTEE_AMOUNT],0))*(IIf(IIf(Day([PAYOUT_DATE])>=Day([GUARANTEE_START_DATE]),0,-1)+(Year([PAYOUT_DATE])-Year([GUARANTEE_START_DATE]))*12+Month([PAYOUT_DATE])-Month([GUARANTEE_START_DATE])<4,1,0)))
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
You really shouldn't use that many IIFs in a query. Instead , either use separate queries,...1 to do one condition,then 1 to do another.
or
use a custom Formula to do the IF,THENs.
 
Last edited:
Upvote 0
Creating a custom function to do the calculations rather than using all the IIF's in the query.

Can you explain what you are trying to achieve with the formula - there could be several other ways to do it.
 
Upvote 0
You could also do such things as:
work with nested or stepwise query results to manipulate your results
break the results into multiple fields that can be easily handled in the next step
use shorter field names
put some of your conditions in the where clause to avoid so many IIFs
 
Upvote 0
So basically what I'm trying to accomplish is that each one of my sales people have 6 month commission guarantees. However, in month's 5 and 6 they get the greater of the two (whether the calculated [PAYOUT_AMT] is higher or the [GUARANTEE_AMOUNT] is higher). I store their guarantee information in a table that consists of their [EMPLOYEE_ID] (which I'm joining on), [GUARANTEE_START_DATE], [GUARANTEE_END_DATE], and [GUARANTEE_AMOUNT]. The [PAYOUT_DATE] is the last day of the prior month (the month I'm paying commissions for). So I need this to dynamically calculate which amount the person gets (the PAYOUT_AMT vs. GUARANTEE_AMOUNT). Also, it needs to be able to cross-over years and still figure out if it's within month's 5 and 6.
 
Upvote 0
As James says the DateDiff function should shorten your expression and remove the requirement to deal with negatives. You should be able to build on something like:

Code:
 IIf(DateDiff("m",[GUARANTEE_START_DATE],[PAYOUT_DATE])>4,IIf([PAYOUT_AMT]>[GUARANTEE_AMOUNT],[PAYOUT_AMT],[GUARANTEE_AMOUNT]),[PAYOUT_AMT])

This will calculate the months between 2 dates even if it spans multiple years.
 
Upvote 0

Forum statistics

Threads
1,221,687
Messages
6,161,287
Members
451,695
Latest member
Doug Mize 1024

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