Results 1 to 11 of 11
  1. #1
    Genzo is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2011
    Posts
    6

    Problem with a IIF statement

    Hi

    Right now im using a command =sum([X]) but if that doesn't return a value it gives back an error which ruins the whole form
    I tried to put in a IIF statement, to check if it returns a value and if it doesn't just set the value to 0, but it doesn't quite work
    here is what i have tries



    Code:
    = IIf([3]; =sum([3]); 0)

  2. #2
    alansidman's Avatar
    alansidman is offline Indifferent
    Windows 7 32bit Access 2007
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,536
    Why don't you explain specifically what you are attempting to do? Is this formula in a form or in a query? Is 3 a field name? Your example is confusing and not clear enough to offer a valid solution. The syntax for an IIF statement is:

    =IIF(Criteria, RESULT IF TRUE, RESULT IF FALSE)

    Your second = sign is not needed.

    Alan

  3. #3
    Genzo is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2011
    Posts
    6
    Im using a danish access version which makes it a bit harder, sorry

    its in a form the [3] refers to a label that get its data through some database.
    If i run the formula =sum([3]) and the label is empty i get an error message that looks like #Name? (translated)

    So what i want that if the label happend to return #name? by using =sum([3]) instead it should say 0(zero) so its a value instead of a string?

  4. #4
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    How does that formula work? you're not comparing the value in your label [3] to anything.

    if you want to check to see if [3] is not null you would do something like:

    =iif(not isnull([3]); sum(nz([3],0));0)

    The NZ function allows you to replace a null value with whatever you want (the default is a 0 I think)

  5. #5
    Genzo is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2011
    Posts
    6
    Quote Originally Posted by rpeare View Post
    How does that formula work? you're not comparing the value in your label [3] to anything.

    if you want to check to see if [3] is not null you would do something like:

    =iif(not isnull([3]); sum(nz([3],0));0)

    The NZ function allows you to replace a null value with whatever you want (the default is a 0 I think)
    Hi i got on the right track thanks to you, but it produces an error, although i was able to get something to work with

    =IIf(IsNumeric([3]);Sum([3]);0)

    but the problem is now, im only allowed it seems, to use it once per form, and i need it the formula at least 4 times in 4 sepperate textboxes
    like so

    =IIf(IsNumeric([1]);Sum([1]);0)
    =IIf(IsNumeric([3]);Sum([3]);0)
    =IIf(IsNumeric([5]);Sum([5]);0)
    =IIf(IsNumeric([7]);Sum([7]);0)

    if i have more then 1, they all fail

  6. #6
    Genzo is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2011
    Posts
    6
    Wouldn't it be better just do it in VBA? on form_load()

    make a script that runs and sets the textboxes to what i need?

  7. #7
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    Ok what exactly are you doing, I expect you are trying to perform a sum on a field that has null values so your sum is coming out as null.

    If that's the case you would have

    =sum(nz([3],0))

    You can't check a single value in a range of values and perform a sum, you basically want to make *all* null values 0 then perform the sum

  8. #8
    Genzo is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2011
    Posts
    6


    as you can see under total i get a :fejl because i didn't remove the =IIf(IsNumeric([1]);Sum([1]);0)

    as you can see the collumn 3 has a #navn because there is no data from the database, and i then do a =sum([3]) i get an error since i can't do that

    the numers top are being joined together from 4 diffrent tabels, and sometimes a code just doesn't exist for a given month.


    How do i make the textboxes return a 0 if there is a #navn in a collumn and the sum([x]) if there is a number

  9. #9
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    The formula I gave you should work. Check the NAME of the calculation field, if it is named for an existing field in your database then you can also get the #name? error. If your summation of field [3] does have a name of a field in your table then just change it and you should get the sum.

  10. #10
    Genzo is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2011
    Posts
    6
    the command didn't work
    =Sum(nz([3];0))

    i get a :fejl which is :error i assume


    I guess there is just no way to do that then

  11. #11
    RayMilhon is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,085
    I have had this type of error in my case it was caused by the Field name and the text box name being the same. I fixed it by going through each txtbox and making sure that they were different. for example if field name is = 3 then the textbox should be txt_3. Then use =Sum(nz([3];0)) or =Sum(nz([txt_3];0)) Assuming this is the problem either would work.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 7
    Last Post: 08-17-2011, 01:49 PM
  2. problem with my DoCmd.RunSQL statement
    By cgclower in forum Access
    Replies: 1
    Last Post: 07-24-2011, 06:12 PM
  3. Me.Filter statement problem.
    By BobG in forum Programming
    Replies: 2
    Last Post: 11-19-2010, 10:25 AM
  4. Replies: 2
    Last Post: 06-14-2010, 03:25 PM
  5. Problem With IF Statement
    By MuskokaMad in forum Programming
    Replies: 0
    Last Post: 03-14-2010, 05:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums