Wrox Programmer Forums
| 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 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
  #1 (permalink)  
Old March 19th, 2004, 01:11 AM
Registered User
 
Join Date: Mar 2004
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Iif statement in Access

I am trying to teach myself Access and can't figure out an Iif statement. I have a field called Status, which uses either P, S, NS, R, or W. I want to calculate the sum of a field called BMUSbmTtl, but only if [Status] <> W. I've tried =Iif([Status]<>"W",sum[BMUSbmTtl])). I get a calculation, but it still calculates all records, including those where status =W.

What am I doing wrong?

Thanks for your help--ms

  #2 (permalink)  
Old March 19th, 2004, 04:28 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

The inline if function needs three parts, an expression to be evaluated as True/False and two statements, the first to return if the expression is True, the second if not. Yours has only one value, there is no value to return or action for the second part.

--

Joe
  #3 (permalink)  
Old March 21st, 2004, 05:36 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Melbourne, Vic, Australia.
Posts: 308
Thanks: 0
Thanked 0 Times in 0 Posts
Default

So, to put it into your context:

=Iif([Status]<>"W",sum([BMUSbmTtl]),-1)

That will give you -1 for cases where status=W

I am a loud man with a very large hat. This means I am in charge
  #4 (permalink)  
Old March 21st, 2004, 05:43 PM
Registered User
 
Join Date: Mar 2004
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks, Steven, but wouldn't the -1 change the outcome of the addition? What I really want the "else" to do is just skip the calculation on that line if it doesn't meet the "W" criteria.

  #5 (permalink)  
Old March 22nd, 2004, 04:55 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Melbourne, Vic, Australia.
Posts: 308
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Unless you're summing that entire expression, it won't change the sum.
So unless you have something like
=Sum(iif([Status]<>"W",Sum([BMUSbmtbl]),-1))
it will work OK.

If you want to have nothing show up, change the -1 to Null
so you'd have:
Code:
=Iif([Status]<>"W",sum([BMUSbmTtl]),Null)

I am a loud man with a very large hat. This means I am in charge
  #6 (permalink)  
Old March 22nd, 2004, 04:57 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Melbourne, Vic, Australia.
Posts: 308
Thanks: 0
Thanked 0 Times in 0 Posts
Default

To clarify even more:

that whole statement, if written into a module, would be:
Code:
If [Status]="W" Then
   intVariable = Sum([BMUSbmtbl)
Else
   intVariable = -1
End If
So it would return either the Sum, or -1

But Null should do you

I am a loud man with a very large hat. This means I am in charge
  #7 (permalink)  
Old March 24th, 2006, 07:26 PM
Registered User
 
Join Date: Mar 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Steven,
I am trying to teach my students how to write IIf functions in queries. Can you tell me how to write IIF that will figure the gross pay for employees that work <=40 Hours or for those that work over 40 hours? Thanks!

Sherry Kay
  #8 (permalink)  
Old March 28th, 2006, 01:14 PM
Friend of Wrox
 
Join Date: Dec 2005
Location: , , .
Posts: 142
Thanks: 0
Thanked 0 Times in 0 Posts
Default

IIf([HoursWorked]<=40,40*HourlyRate,40*[HourlyRate]+([HoursWorked]-40)*[OTRate])



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I make this a IIF Statement Corey Access VBA 1 November 3rd, 2006 08:40 AM
IIf Statement "division by zero" error Corey BOOK: Beginning Access 2003 VBA 5 September 30th, 2005 05:48 AM
IIF Statement golden Access 2 August 3rd, 2004 01:32 AM
IIF Statement golden Access 3 July 12th, 2004 07:49 PM
IIF Statement Question fastcorvette Access 8 October 31st, 2003 11:01 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.