Wrox Programmer Forums Calculation
 |
 Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
 Welcome to the p2p.wrox.com Forums. You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com

February 14th, 2004, 09:11 PM
 Grantm Registered User Join Date: Feb 2004 Posts: 2 Thanks: 0 Thanked 0 Times in 0 Posts
Calculation

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!)

February 14th, 2004, 09:16 PM
 Dataman Authorized User Join Date: Jun 2003 Posts: 75 Thanks: 0 Thanked 0 Times in 0 Posts

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

February 15th, 2004, 05:26 PM
 Steven Friend of Wrox Join Date: Jun 2003 Posts: 308 Thanks: 0 Thanked 0 Times in 0 Posts

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:
Code:
```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)

February 16th, 2004, 10:14 AM
 Grantm Registered User Join Date: Feb 2004 Posts: 2 Thanks: 0 Thanked 0 Times in 0 Posts

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.

 Similar Threads Thread Thread Starter Forum Replies Last Post Calculation in access ashik112 Access 1 February 26th, 2007 08:38 AM Time Calculation surendran MySQL 1 June 24th, 2006 01:38 PM Calculation dillemma.... cuzintone SQL Language 0 April 7th, 2005 03:59 PM Time calculation rdfernandez Access VBA 6 March 24th, 2005 11:51 AM How to do calculation between 2 tables hwsc SQL Language 2 January 13th, 2005 09:29 PM