Results 1 to 5 of 5
  1. #1
    sandydaly is offline Novice
    Windows 10 Access 2007
    Join Date
    Dec 2017
    Location
    Sunrise, FL
    Posts
    4

    Need Help with long Nested IIF Expression PLEASE :)

    I'm trying to add an IF to my already existing expression in my database. I'm trying to add MG to calculate as a new UOM. The expression works and calculates correctly without the addition of the final IF of the "MG" uom. It keeps giving me the error that my expression contains the wrong number of arguments. I tried adding a ) to the end of the expression and that didn't work. I tried deleting one and still no go. Any help would be greatly appreciated.




    Amt Base: IIf([Controlled Substance Name]="Morphine Oxycodone Mix",0,IIf([Controlled Substance Name]="Butalbital Codeine Mix",0,IIf([Controlled Substance Name]="Amphetamine WIP FIN",0,IIf([UOM]="EA",[Quantity]*[Number of Tabs or Caps per EA]*[Strength]/1000*[Conversion Factor],IIf([UOM]="KG",[Quantity]*[Percent Active]*1000*[Conversion Factor],[Quantity]*[Percent Active]*[Conversion Factor],IIf([UOM]="MG",[Quantity]/1000*[Percent Active]*[Conversion Factor]))))))

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,917
    Since there is a limit to nested IIF's (seven I think) and they can be very difficult to read sometimes I use a public SELECT CASE structure procedure in a Standard Module. Easier to read and document.

  3. #3
    Join Date
    Apr 2017
    Posts
    1,792
    Too many nested IIf's. There is a limit on them.

    Workarounds:

    1. Design an UDF, which returns needed value;
    2. Group IIf's with same outcome. Like
    Code:
    IIf([Controlled Substance Name]="Morphine Oxycodone Mix" OR 
    [Controlled Substance Name]="Butalbital Codeine Mix" OR 
    [Controlled Substance Name]="Amphetamine WIP FIN",0,
    IIf([UOM]="EA",[Quantity]*[Number of Tabs or Caps per EA]*[Strength]/1000*[Conversion Factor],
    IIf([UOM]="KG",[Quantity]*[Percent Active]*1000*[Conversion Factor],[Quantity]*[Percent Active]*[Conversion Factor],
    IIf([UOM]="MG",[Quantity]/1000*[Percent Active]*[Conversion Factor])))
    
    3. Group IIfs structually. Like
    Code:
    IIf([Controlled Substance Name]="Morphine Oxycodone Mix" OR 
    [Controlled Substance Name]="Butalbital Codeine Mix" OR 
    [Controlled Substance Name]="Amphetamine WIP FIN",
    IIf([Controlled Substance Name]="Morphine Oxycodone Mix",0,
    IIF([Controlled Substance Name]="Butalbital Codeine Mix",0,0)),
    Iif([UOM]="EA",[Quantity]*[Number of Tabs or Caps per EA]*[Strength]/1000*[Conversion Factor],
    IIf([UOM]="KG",[Quantity]*[Percent Active]*1000*[Conversion Factor],[Quantity]*[Percent Active]*[Conversion Factor],[Quantity]/1000*[Percent Active]*[Conversion Factor])))
    4. Create a table where all possible combinations and matching return values are defined. Use DLookup to get the value from lookup table.

  4. #4
    sandydaly is offline Novice
    Windows 10 Access 2007
    Join Date
    Dec 2017
    Location
    Sunrise, FL
    Posts
    4
    Thanks! I have never used one of those before. I have used modules to run Marcos in Access but not in a query builder. Will it work the same in the query builder or do I have to link the query to a module to make it calculate the correct UOM in the query? All of my queries are tied to these IIF statements with the calculations are reports. Do I type the formula directly into the expression builder? Is this how I would write it?

    Select Case performance
    Case 1
    UOM = “EA”,[Quantity]*[Number of Tabs or Caps per EA]*[Strength]/1000*[Conversion Factor]
    Case 2
    UOM ="KG",[Quantity]*[Percent Active]*1000*[Conversion Factor],[Quantity]*[Percent Active]*[Conversion Factor]
    Case 3
    UOM="MG",[Quantity]/1000*[Percent Active]*[Conversion Factor]
    Case Is 4
    [Controlled Substance Name]="Morphine Oxycodone Mix"=0, [Controlled Substance Name]="Butalbital Codeine Mix"=0, [Controlled Substance Name]="Amphetamine WIP FIN"=0,
    Case Else

    End Select
    End Function

  5. #5
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,741
    The user defined function suggested by Arvilaanenets would look something like this:
    The outer select case selects the drug, and the inner select case deals with the UOM.

    Code:
    Public Function calcAmtBase(argDrug as string, argUOM as string) as double
    Select case argDrug
        Case "Morphine Oxycodone Mix", Butalbital Codeine Mix", "Amphetamine WIP FIN"
            Select Case argUOM
                Case "EA"
                     calcAmtBase = [Quantity]*[Number of Tabs or Caps per EA]*[Strength]/1000*[Conversion Factor]
                Case "KG"
                     calcAmtBase = [Quantity]*[Percent Active]*1000*[Conversion Factor]
                Case "MG"
                     calcAmtBase = ......
                Case Else
                     calcAmtBase = 'whatever for no matches
           End Select     'end checking UOM  for drugs above   
        Case              ' any other drugs
         .
         . 
    End Select ' end checking for drug names
    In your expression, when you want the Amt Base call the function like:
    Code:
    calcAmtBase([Controlled Substance Name],UOM)

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

Similar Threads

  1. Replies: 2
    Last Post: 11-16-2017, 03:33 AM
  2. Replies: 4
    Last Post: 05-03-2016, 01:51 PM
  3. Replies: 3
    Last Post: 07-10-2015, 01:19 PM
  4. nested IIF BETWEEN AND EXPRESSION
    By sichilaba in forum Forms
    Replies: 1
    Last Post: 03-30-2015, 03:36 AM
  5. Replies: 11
    Last Post: 11-17-2010, 03:43 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