Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 14th, 2004, 09:11 PM
Registered User
 
Join Date: Feb 2004
Location: Milwaukee, WI, USA.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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!)
Reply With Quote
  #2 (permalink)  
Old February 14th, 2004, 09:16 PM
Authorized User
 
Join Date: Jun 2003
Location: Glendale, AZ, USA.
Posts: 75
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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 With Quote
  #3 (permalink)  
Old February 15th, 2004, 05:26 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Melbourne, Vic, Australia.
Posts: 308
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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)
Reply With Quote
  #4 (permalink)  
Old February 16th, 2004, 10:14 AM
Registered User
 
Join Date: Feb 2004
Location: Milwaukee, WI, USA.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 10:00 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.