PC Review


Reply
Thread Tools Rate Thread

Can you help me make this string shorter?

 
 
Alberta Rose
Guest
Posts: n/a
 
      3rd Mar 2010
I get an error message saying that my string is too complex. Can anyone see
a way to streamline it??

=IIf(IsNull([costcode]) Or IsNull([costtype]),0,IIf([costcode]="013210" And
[costtype]="05320",[txtEstimatedCost]/[txtCraftLabEst],IIf([costcode]="020110"
And
[costtype]="05320",[txtEstimatedCost]/[txtFirewatchLabEst],IIf([costcode]="064201"
And
[costtype]="05320",[txtEstimatedCost]/[txtCraftSuprLabEst],IIf([costcode]="061301"
And
[costtype]="05320",[txtEstimatedCost]/[txtQAQCSuprvLabEst],IIf([costcode]="061101"
And
[costtype]="05310",[txtEstimatedCost]/[txtSiteTeamLabEst],IIf([costcode]="031101"
And
[costtype]="05110",[txtEstimatedCost]/[txtOfficeTeamLabEst],IIf([costcode]="042211"
And
[costtype]="05110",[txtEstimatedCost]/[txtEngLabEst],IIf([costcode]="045311"
And
[costtype]="05130",[txtEstimatedCost]/[txtThirdPartyLabEst],IIf([costcode]="032101"
And [costtype]="05110",[txtEstimatedCost]/[txtProcurementLabEst],
IIf([costcode]="061103" And
[costtype]="05110",[txtEstimatedCost]/[txtClericalLabEst],
IIf([costcode]="032101" And
[costtype]="05130",[txtEstimatedCost]/[txt3rdPtyProcLabEst] ,
IIf([costcode]="045311" And
[costtype]="05110",[txtEstimatedCost]/[txtEngMgmtLabEst],
IIf([costcode]="061201" And
[costtype]="05320",[txtEstimatedCost]/[txtHSSEEst], IIf([costcode]="064101"
And [costtype]="05320",[txtEstimatedCost]/[txtSiteConMgmtEst],
Null)))))))))))))))

Thanks, Laurie
 
Reply With Quote
 
 
 
 
Jack Leach
Guest
Posts: n/a
 
      3rd Mar 2010
Hi Laurie,

I find that rather than using a massive amount of nested Iif statements in
source string or a query, sometimes you're better off creating a public
function that will handle the crunchwork for you. I haven't deciphered
exactly what you're trying to accomplish here, but it looks like this might
be a good canidate for the concept.

In a standard module, declare a function as Public and you can then have it
return the required value (what your Iif statements would otherwise
ultimately return). Reference it in a control source or query just like you
would a builtin function: =FunctionName(arg arg arg arg).

At quick glance at your calculation, it looks like you would pass a number
of the fields as arguments to the function. Then you can convert all of your
Iifs into the more code friendly If/Then/Else statements inside the function.
It might take a lot of If/Then statements, but will probably be more
efficient than the way you are going about it now.

Note that in order to return a function as Null the datatype of the function
needs to be declared as a Variant.


Public Function GetThisValue( _
sCostType As String, _
cEstimatedCost As Currency, _
cFirewatchLabEst As Currency, _
etc etc) As Variant

'do all your calculations here and return the
'appropriate value to the function


End Function



and call it from your control source or query like so:

=GetThisValue([costtype], [txtEstimatedCost], etc)



hth

--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)



"Alberta Rose" wrote:

> I get an error message saying that my string is too complex. Can anyone see
> a way to streamline it??
>
> =IIf(IsNull([costcode]) Or IsNull([costtype]),0,IIf([costcode]="013210" And
> [costtype]="05320",[txtEstimatedCost]/[txtCraftLabEst],IIf([costcode]="020110"
> And
> [costtype]="05320",[txtEstimatedCost]/[txtFirewatchLabEst],IIf([costcode]="064201"
> And
> [costtype]="05320",[txtEstimatedCost]/[txtCraftSuprLabEst],IIf([costcode]="061301"
> And
> [costtype]="05320",[txtEstimatedCost]/[txtQAQCSuprvLabEst],IIf([costcode]="061101"
> And
> [costtype]="05310",[txtEstimatedCost]/[txtSiteTeamLabEst],IIf([costcode]="031101"
> And
> [costtype]="05110",[txtEstimatedCost]/[txtOfficeTeamLabEst],IIf([costcode]="042211"
> And
> [costtype]="05110",[txtEstimatedCost]/[txtEngLabEst],IIf([costcode]="045311"
> And
> [costtype]="05130",[txtEstimatedCost]/[txtThirdPartyLabEst],IIf([costcode]="032101"
> And [costtype]="05110",[txtEstimatedCost]/[txtProcurementLabEst],
> IIf([costcode]="061103" And
> [costtype]="05110",[txtEstimatedCost]/[txtClericalLabEst],
> IIf([costcode]="032101" And
> [costtype]="05130",[txtEstimatedCost]/[txt3rdPtyProcLabEst] ,
> IIf([costcode]="045311" And
> [costtype]="05110",[txtEstimatedCost]/[txtEngMgmtLabEst],
> IIf([costcode]="061201" And
> [costtype]="05320",[txtEstimatedCost]/[txtHSSEEst], IIf([costcode]="064101"
> And [costtype]="05320",[txtEstimatedCost]/[txtSiteConMgmtEst],
> Null)))))))))))))))
>
> Thanks, Laurie

 
Reply With Quote
 
John W. Vinson
Guest
Posts: n/a
 
      3rd Mar 2010
On Wed, 3 Mar 2010 11:45:01 -0800, Alberta Rose
<(E-Mail Removed)> wrote:

>I get an error message saying that my string is too complex. Can anyone see
>a way to streamline it??


Two suggestions:

1. Use the Switch() function rather than nested IIF's. Switch() takes
arguments in pairs, and evaluates them left to right; when it first encounters
a pair in which the first element is TRUE, it returns the second element of
the pair and quits. So:

=Switch(IsNull([costcode] OR IsNull([costtype], 0,
[costcode] = "013210" AND [costtype] = "05320",
[txtEstimatedCost]/[txtCraftLabEst],

<etc>

2. and much better: normalize your tables so that you don't have to store so
much business logic in complex code!!! I don't clearly understand the business
logic but it certainly seems like you have a whole lot of field ending in Est,
which probably should not be fields in your table, but *records* in another
table (which would also have costtype and costcode fields so it could be
simply joined to this table in a Query).

>
>=IIf(IsNull([costcode]) Or IsNull([costtype]),0,IIf([costcode]="013210" And
>[costtype]="05320",[txtEstimatedCost]/[txtCraftLabEst],IIf([costcode]="020110"
>And
>[costtype]="05320",[txtEstimatedCost]/[txtFirewatchLabEst],IIf([costcode]="064201"
>And
>[costtype]="05320",[txtEstimatedCost]/[txtCraftSuprLabEst],IIf([costcode]="061301"
>And
>[costtype]="05320",[txtEstimatedCost]/[txtQAQCSuprvLabEst],IIf([costcode]="061101"
>And
>[costtype]="05310",[txtEstimatedCost]/[txtSiteTeamLabEst],IIf([costcode]="031101"
>And
>[costtype]="05110",[txtEstimatedCost]/[txtOfficeTeamLabEst],IIf([costcode]="042211"
>And
>[costtype]="05110",[txtEstimatedCost]/[txtEngLabEst],IIf([costcode]="045311"
>And
>[costtype]="05130",[txtEstimatedCost]/[txtThirdPartyLabEst],IIf([costcode]="032101"
>And [costtype]="05110",[txtEstimatedCost]/[txtProcurementLabEst],
>IIf([costcode]="061103" And
>[costtype]="05110",[txtEstimatedCost]/[txtClericalLabEst],
>IIf([costcode]="032101" And
>[costtype]="05130",[txtEstimatedCost]/[txt3rdPtyProcLabEst] ,
>IIf([costcode]="045311" And
>[costtype]="05110",[txtEstimatedCost]/[txtEngMgmtLabEst],
>IIf([costcode]="061201" And
>[costtype]="05320",[txtEstimatedCost]/[txtHSSEEst], IIf([costcode]="064101"
>And [costtype]="05320",[txtEstimatedCost]/[txtSiteConMgmtEst],
> Null)))))))))))))))
>
>Thanks, Laurie

--

John W. Vinson [MVP]
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
This could be shorter tight? (String to Hex) Edwin Knoppert Microsoft ASP .NET 2 17th Feb 2006 05:00 PM
I wonder if you can make this shorter =?Utf-8?B?VA==?= Microsoft Excel Worksheet Functions 0 23rd Dec 2004 08:47 PM
Re: make this shorter please! CLR Microsoft Excel Worksheet Functions 0 6th Sep 2003 10:30 PM
Re: make this shorter please! Aladin Akyurek Microsoft Excel Worksheet Functions 0 6th Sep 2003 10:11 PM
Re: make this shorter please! Don Guillett Microsoft Excel Worksheet Functions 1 6th Sep 2003 09:12 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:53 PM.