Subject: Calculation
Posted By: Grantm Post Date: 2/14/2004 8:11:46 PM
I think this is one of those times when I'm missing the obvious.  I want a range of numbers in a bound text box to return a number in another bound text box in the same form. Ex: If [FinanceAmount]is between 1 and 30, [DailyCost]returns "1", if [FinanceAmount]is between 31 and 60, [DailyCost]returns "2", and so on.  I've been able to do it for one level only using an IIF expression, but could not get it to go further.  I done much more complicated calculations and it seems I should know the answer to this, but I just keep drawing a blank.  What am I missing. (Be gentle!)
Reply By: Dataman Reply Date: 2/14/2004 8:16:40 PM
Hi there,
   Here is one possible solution.

   Put this in the after update of the first text box.

   SELECT CASE me.txtFirstTextBox
   Case 1 to 30
     Me.txtSecondTextBox = 1
   Case 21 to 60
     Me.txtSecondTextBox = 2
   'you get the picture
   End Select

HTH,
Mike
Reply By: Steven Reply Date: 2/15/2004 4:26:47 PM
Or - if you prefer to keep using the IIF expression, you could do this:

iif([condition1],[returnvalue1],iif([condition2],[returnvalue2],iif([condition3],[returnvalue3],[returnvalue4])))

Basically, if you want to nest iif statements, just keep on adding new iif statements to the "else" part of the statement. The main thing you have to make sure you do, however, is to have an "else" statement right at the end ([returnvalue4] in the above example), and have the right amount of closing parentheses, which is easy to do: just count the number of times "iif" shows up in your expression, and put that many closing parentheses at the end (in my example, "iif" is in there 3 times, so there's 3 closing parentheses)

But, especially given that you're using this in a form, I would do as Mike says, and use a Select Case statement. You should only nest "iif" statements if you cant use Select Case statements.
But as a fallback, you should also always have a "Case Else" part in your statement, in case something odd happens, such as:
SELECT CASE me.txtFirstTextBox
   Case 1 to 30
     Me.txtSecondTextBox = 1
   Case 21 to 60
     Me.txtSecondTextBox = 2
   'And so on
   Case Else
     Me.txtSecondTextBox = -1
     MsgBox "Oh no! something bad happened! Oh what a world"
   End Select

HTH

Steven


I am a loud man with a very large hat. This means I am in charge
(Edited due to typo)
Reply By: Grantm Reply Date: 2/16/2004 9:14:13 AM
Dataman and Steven,

Both solutions seem obvious to me NOW.  I've used the SELECT CASE Statement with Case Else and it does the job perfectly.  Thanks for the help.

Go to topic 9806

Return to index page 948
Return to index page 947
Return to index page 946
Return to index page 945
Return to index page 944
Return to index page 943
Return to index page 942
Return to index page 941
Return to index page 940
Return to index page 939