Results 1 to 10 of 10
  1. #1
    FoolzRailer is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Nov 2015
    Posts
    51

    Alternativ for too many multiple Iif statements?

    Hello



    I'm trying to count if a certain criteria is met, in this instance if RB is true, it should count it. Now I've got this to work the way it should, when I just had to count based on a few criteria. However the below example exceeds the amount of Iif statements allowed. So my question is, is there another way of doing what i specified without using Iif?

    Code:
    SELECT Sanering_af_hoved_og_stikledninger.LedningsID, Sanering_af_hoved_og_stikledninger.OpstroemKnudeID, Sanering_af_hoved_og_stikledninger.OpstrømsKnudeNavn, Sanering_af_hoved_og_stikledninger.Opstrømsdybde, Sanering_af_hoved_og_stikledninger.NedstroemKnudeID, Sanering_af_hoved_og_stikledninger.Nedstrømsdybde, Sanering_af_hoved_og_stikledninger.NedstrømsKnudeNavn, Sanering_af_hoved_og_stikledninger.Laengde, Sanering_af_hoved_og_stikledninger.Handelsmaal, Sanering_af_hoved_og_stikledninger.Materiale, Sanering_af_hoved_og_stikledninger.TVObsKode, 
    
    Count(IIf([TVObs].[TVObsKode]="RB",True,IIf([TVObs].[TVObsKode]="OB",True,IIf([TVObs].[TVObsKode]="PF",True,IIf([TVObs].[TVObsKode]="DE",True,IIf([TVObs].[TVObsKode]="FS",True,IIf([TVObs].[TVObsKode]="IS",True,IIf([TVObs].[TVObsKode]="RØ",True,IIf([TVObs].[TVObsKode]="IN",True,IIf([TVObs].[TVObsKode]="AF",True,IIf([TVObs].[TVObsKode]="BE",True,IIf([TVObs].[TVObsKode]="FO",True,IIf([TVObs].[TVObsKode]="GR",True,IIf([TVObs].[TVObsKode]="PH",True,IIf([TVObs].[TVObsKode]="PB",True,IIf([TVObs].[TVObsKode]="OS",True,IIf([TVObs].[TVObsKode]="OP",True,IIf([TVObs].[TVObsKode]="OK",True,Null)))))))))))))))))) AS [Antal Observationer]
    
    FROM Rorenes_tilstand, Sanering_af_hoved_og_stikledninger
    
    GROUP BY Sanering_af_hoved_og_stikledninger.LedningsID, Sanering_af_hoved_og_stikledninger.OpstroemKnudeID, Sanering_af_hoved_og_stikledninger.OpstrømsKnudeNavn, Sanering_af_hoved_og_stikledninger.Opstrømsdybde, Sanering_af_hoved_og_stikledninger.NedstroemKnudeID, Sanering_af_hoved_og_stikledninger.Nedstrømsdybde, Sanering_af_hoved_og_stikledninger.NedstrømsKnudeNavn, Sanering_af_hoved_og_stikledninger.Laengde, Sanering_af_hoved_og_stikledninger.Handelsmaal, Sanering_af_hoved_og_stikledninger.Materiale, Sanering_af_hoved_og_stikledninger.TVObsKode
    
    ORDER BY Sanering_af_hoved_og_stikledninger.LedningsID;
    Picture belows shows, that the marked LedningsID 411 has 1 FS and 1 RB. Where as LedningsID 391 has 2 RB.

    Click image for larger version. 

Name:	Udklip.PNG 
Views:	13 
Size:	104.9 KB 
ID:	24793

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    google

    'access vba switch function'

    and

    'access vba choose function'

    for some alternatives

    also strongly advise you use aliasing to reduce the size of the table names to make the code more readable

    FROM Rorenes_tilstand AS R, Sanering_af_hoved_og_stikledninger AS S

  3. #3
    orange's Avatar
    orange is online now Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Picture belows shows, that the marked LedningsID 411 has 1 FS and 1 RB. Where as LedningsID 391 has 2 RB.
    According to the picture both 411 and 391 have 1 FS and 1RB????

  4. #4
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    What is the information you are trying to get here? It looks like you are trying to find all LedningsID that have duplicate values for TVObsKode - is that correct?

  5. #5
    FoolzRailer is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Nov 2015
    Posts
    51
    For a ledningsID lets say 391, I need to count how many times there is an RB, PH, FS, IS or VA. In the above picture, it has countet that ledningsID has 1 FS and 2 RB.
    In a following query (which I haven't shown) it concatenates the data, so there is only one instance of 391 with one field that says "FS: 1, RB: 2".

    This all works when I only have a small amount of Iifs as above. But when I want to add that it should also count RØ, PR, SO, KB, LM (or something similar) the expression gets too complex.


    To translate the above:
    LedningsID = Pipe ID
    RB = Cracks in pipes

    So a pipe can have several cracks and many pipes can have cracks (RB).

    Hope this helps to clarify?

  6. #6
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    This all works when I only have a small amount of Iifs as above. But when I want to add that it should also count RØ, PR, SO, KB, LM (or something similar) the expression gets too complex.
    did you read post#2?

  7. #7
    FoolzRailer is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Nov 2015
    Posts
    51
    #6 Just did a quick Google, but got dumped with a lot of work at my job. Will give it a thorough look over the weekend, but I have to admit VBA is not my strong side

  8. #8
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Ok, I think I see what you want. You need a query which produces the output in the picture, am I right? In that case its easy, you don't need an Iif at all, all you need is a Count:

    Code:
    SELECT Sanering_af_hoved_og_stikledninger.LedningsID, Sanering_af_hoved_og_stikledninger.OpstroemKnudeID, Sanering_af_hoved_og_stikledninger.OpstrømsKnudeNavn, Sanering_af_hoved_og_stikledninger.Opstrømsdybde, Sanering_af_hoved_og_stikledninger.NedstroemKnudeID, Sanering_af_hoved_og_stikledninger.Nedstrømsdybde, Sanering_af_hoved_og_stikledninger.NedstrømsKnudeNavn, Sanering_af_hoved_og_stikledninger.Laengde, Sanering_af_hoved_og_stikledninger.Handelsmaal, Sanering_af_hoved_og_stikledninger.Materiale, Sanering_af_hoved_og_stikledninger.TVObsKode, count(TVObsKote)AS [Antal Observationer]
    
    
    FROM Rorenes_tilstand, Sanering_af_hoved_og_stikledninger
    
    GROUP BY Sanering_af_hoved_og_stikledninger.LedningsID, Sanering_af_hoved_og_stikledninger.OpstroemKnudeID, Sanering_af_hoved_og_stikledninger.OpstrømsKnudeNavn, Sanering_af_hoved_og_stikledninger.Opstrømsdybde, Sanering_af_hoved_og_stikledninger.NedstroemKnudeID, Sanering_af_hoved_og_stikledninger.Nedstrømsdybde, Sanering_af_hoved_og_stikledninger.NedstrømsKnudeNavn, Sanering_af_hoved_og_stikledninger.Laengde, Sanering_af_hoved_og_stikledninger.Handelsmaal, Sanering_af_hoved_og_stikledninger.Materiale, Sanering_af_hoved_og_stikledninger.TVObsKode
    
    ORDER BY Sanering_af_hoved_og_stikledninger.LedningsID;
    I have highlighted the change in bold.

    I think this should work for you.

  9. #9
    FoolzRailer is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Nov 2015
    Posts
    51
    Hello again, it appears this did the trick, if you have more comments/suggestions on alternatives (still looking into the VBA later) I'm open for smarter ways

    Sum(IIf([TVObs].[TVObsKode] IN ("RB", "FO", "FS", "IN", "IS", "FS", "PH", "RØ", "ÅS", "VA", "OB", "PF", "DE", "AF", "BE", "GR", "PB", "OS", "OP", "OK"),1,0)) AS [Antal Observationer]


    @#8 I was typing as you were, but yes that is exactly what I was going for. I will look at your suggestion, thanks for the help

  10. #10
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    If you want the total number of observations for each LedningsID, but don't need it broken down by different TVObsCode values, jusr remove TVObsCode from the Select and Group by parts:

    Code:
    SELECT Sanering_af_hoved_og_stikledninger.LedningsID, Sanering_af_hoved_og_stikledninger.OpstroemKnudeID, Sanering_af_hoved_og_stikledninger.OpstrømsKnudeNavn, Sanering_af_hoved_og_stikledninger.Opstrømsdybde, Sanering_af_hoved_og_stikledninger.NedstroemKnudeID, Sanering_af_hoved_og_stikledninger.Nedstrømsdybde, Sanering_af_hoved_og_stikledninger.NedstrømsKnudeNavn, Sanering_af_hoved_og_stikledninger.Laengde, Sanering_af_hoved_og_stikledninger.Handelsmaal, Sanering_af_hoved_og_stikledninger.Materiale, Sanering_af_hoved_og_stikledninger.TVObsKode, count(TVObsKote)AS [Antal Observationer]
    
    
    FROM Rorenes_tilstand, Sanering_af_hoved_og_stikledninger
    
    GROUP BY Sanering_af_hoved_og_stikledninger.LedningsID, Sanering_af_hoved_og_stikledninger.OpstroemKnudeID, Sanering_af_hoved_og_stikledninger.OpstrømsKnudeNavn, Sanering_af_hoved_og_stikledninger.Opstrømsdybde, Sanering_af_hoved_og_stikledninger.NedstroemKnudeID, Sanering_af_hoved_og_stikledninger.Nedstrømsdybde, Sanering_af_hoved_og_stikledninger.NedstrømsKnudeNavn, Sanering_af_hoved_og_stikledninger.Laengde, Sanering_af_hoved_og_stikledninger.Handelsmaal, Sanering_af_hoved_og_stikledninger.Materiale, Sanering_af_hoved_og_stikledninger.TVObsKode
    
    ORDER BY Sanering_af_hoved_og_stikledninger.LedningsID;
    Remove the parts indicated in Red to get:

    Code:
    SELECT Sanering_af_hoved_og_stikledninger.LedningsID, Sanering_af_hoved_og_stikledninger.OpstroemKnudeID, Sanering_af_hoved_og_stikledninger.OpstrømsKnudeNavn, Sanering_af_hoved_og_stikledninger.Opstrømsdybde, Sanering_af_hoved_og_stikledninger.NedstroemKnudeID, Sanering_af_hoved_og_stikledninger.Nedstrømsdybde, Sanering_af_hoved_og_stikledninger.NedstrømsKnudeNavn, Sanering_af_hoved_og_stikledninger.Laengde, Sanering_af_hoved_og_stikledninger.Handelsmaal, Sanering_af_hoved_og_stikledninger.Materiale, count(TVObsKote)AS [Antal Observationer]
    
    
    FROM Rorenes_tilstand, Sanering_af_hoved_og_stikledninger
    
    GROUP BY Sanering_af_hoved_og_stikledninger.LedningsID, Sanering_af_hoved_og_stikledninger.OpstroemKnudeID, Sanering_af_hoved_og_stikledninger.OpstrømsKnudeNavn, Sanering_af_hoved_og_stikledninger.Opstrømsdybde, Sanering_af_hoved_og_stikledninger.NedstroemKnudeID, Sanering_af_hoved_og_stikledninger.Nedstrømsdybde, Sanering_af_hoved_og_stikledninger.NedstrømsKnudeNavn, Sanering_af_hoved_og_stikledninger.Laengde, Sanering_af_hoved_og_stikledninger.Handelsmaal, Sanering_af_hoved_og_stikledninger.Materiale
    ORDER BY Sanering_af_hoved_og_stikledninger.LedningsID;

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

Similar Threads

  1. Multiple iif Statements in 1 Exp
    By dmd in forum Queries
    Replies: 16
    Last Post: 12-01-2015, 08:15 AM
  2. Replies: 2
    Last Post: 09-04-2013, 12:11 PM
  3. Multiple iif statements
    By rlsublime in forum Queries
    Replies: 3
    Last Post: 04-25-2012, 10:33 AM
  4. Multiple SQL Statements
    By springboardjg in forum Queries
    Replies: 1
    Last Post: 04-18-2011, 10:32 AM
  5. Multiple IIF statements
    By KevinMCB in forum Queries
    Replies: 4
    Last Post: 12-03-2010, 01:35 PM

Tags for this Thread

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