IIF() Statement in Query based on Option Value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • reginaldmerritt
    New Member
    • Nov 2006
    • 201

    IIF() Statement in Query based on Option Value

    I have a table TBStaff with boolean fields to show their type of employment. Staff can have many types of employment (upto 6).

    TBStaff
    StaffID________ _Autonumber (PK)
    Forenames_____T ext
    Surname_______T ext
    Assessor_______ yes/no
    Trainer________ _yes/no
    IV_____________ yes/no
    etc.

    I have a form which lists all the staff (FRMStaffList). I've put option controls on the form for the user to select which type of employment they want to see.

    [O].Assessor....(c ontrol named "AssessorChosen ")
    [O].Trainer....... .(control named "TrainerChosen" )
    [O].IV............ ...(control named "IVChosen")


    In the query used for FRMStaffList I could simply put the criteria for the boolean fields as Forms!FRMStaffL ist.AssessorCho sen

    i.e.
    Code:
    SELECT TBStaff.Assessor, TBStaff.Trainer, TBStaff.IV
    FROM TBStaff
    WHERE (((TBStaff.Assessor)=[Forms]![FRMStaffList].[AssessorChosen]) AND 
          ((TBStaff.Trainer)=[Forms]![FRMStaffList].[TrainerChosen]) AND 
          ((TBStaff.IV)=[Forms]![FRMStaffList].[IVChosen]));
    However, if I do this for all employment types the query will only look for single employment types.

    Consider example below:
    Code:
    [b]Name           Assessor     Trainer    IV [/b]
    Bill Hicks      True
    Bill Bailey     True         True
    Bill Gates      True                   True
    Bill Murry                   True
    If I wanted to show just Assessors the query will will evaluate as
    Code:
    SELECT TBStaff.Assessor, TBStaff.Trainer, TBStaff.IV
    FROM TBStaff
    WHERE (((TBStaff.Assessor)=TRUE) AND
          ((TBStaff.Trainer)=FALSE]) AND 
          ((TBStaff.IV)=FALSE));
    This will show only Bill Hicks and not Bill Bailey and Bill Gates.

    I tried to use an IIF statement to choose the criteria statement based on the options the user chooses.

    e.g.
    Code:
    SELECT TBStaffTmp.TBStaffID, TBStaffTmp.Forenames, TBStaffTmp.Assessor, TBStaffTmp.Trainer, TBStaffTmp.IV
    FROM TBStaffTmp
    WHERE (((TBStaffTmp.Assessor)=IIf([Forms]![FRMStaffListTMP].[AssessorChosen]=True,True,True Or False)) AND 
          ((TBStaffTmp.Trainer)=IIf([Forms]![FRMStaffListTMP].[TrainerChosen]=True,True,True Or False)) AND 
          ((TBStaffTmp.IV)=IIf([Forms]![FRMStaffListTMP].[IVChosen]=True,True,True Or False)));


    But this doesn't work, can IIF statements even be used in this way?

    Of course I guess I could create a different query based on the options the user chooses but I would have to create an unbelievable amount of queries to do that.

    Any help much appreciated.
  • reginaldmerritt
    New Member
    • Nov 2006
    • 201

    #2
    OK I think I have it sorted. Not sure if this is the best way of going about this. In essence I wanted to show records from TBStaff where the boolean fields where true according to the options chosen by the user. However, passing the option control value though to the queries means passing unwanted criteria, i.e. showing records because boolean value is False.

    I have created 6 queries all based on one single boolean field.
    e.g.
    Code:
    SELECT TBStaffTmp.StaffID, TBStaffTmp.Forenames, TBStaffTmp.Assessor, TBStaffTmp.Trainer, TBStaffTmp.IV
    FROM TBStaffTmp
    WHERE (((TBStaffTmp.Assessor)=[Forms]![FRMStaffListTMP].[AssessorChosen]));
    Then
    Code:
    SELECT TBStaffTmp.StaffID, TBStaffTmp.Forenames, TBStaffTmp.Assessor, TBStaffTmp.Trainer, TBStaffTmp.IV
    FROM TBStaffTmp
    WHERE (((TBStaffTmp.Trainer)=[Forms]![FRMStaffListTMP].[TrainerChosen]));
    and
    Code:
    SELECT TBStaffTmp.StaffID, TBStaffTmp.Forenames, TBStaffTmp.Assessor, TBStaffTmp.Trainer, TBStaffTmp.IV
    FROM TBStaffTmp
    WHERE (((TBStaffTmp.IV)=[Forms]![FRMStaffListTMP].[IVChosen]));
    e.c.t.

    I then used a union query to put the records together. However, I only want to union the tables if the relevant option has been chosen by the user.

    For example, if user chooses to view IV's only then the first query will evaluate to
    Code:
    SELECT TBStaffTmp.TBStaffID, TBStaffTmp.Forenames, TBStaffTmp.Assessor, TBStaffTmp.Trainer, TBStaffTmp.IV
    FROM TBStaffTmp
    WHERE (((TBStaffTmp.Assessor)=FALSE));
    This would show record BILL MURRY as that record does have TBStaffTmp.Asse ssor value of False.

    What I have to do is Union only those records that show a relevant record. I've done this by adding a WHERE statement.
    Code:
    SELECT TBStaffID, Forenames, Assessor, Trainer, IV
    FROM QYStaffListTMP1
    WHERE Assessor = True
    UNION ALL
    SELECT TBStaffID, Forenames, Assessor, Trainer, IV
    FROM QYStaffListTMP2
    WHERE Trainer = True
    UNION ALL SELECT TBStaffID, Forenames, Assessor, Trainer, IV
    FROM QYStaffListTMP3
    WHERE IV = True;
    I then added the results of that Union query to another query run as DISTINCT as duplicate records are very likely.

    I think logically that is correct, if anyone notices anything wrong with this or has a better solution please let me know.

    Comment

    • Mihail
      Contributor
      • Apr 2011
      • 759

      #3
      Hi !
      First of all, your database is as much non-normalized that not allows me to think on it.
      I advice you to take a look here:


      Anyway, I sketch a normalized database for you and I do your job (in my way, of course)
      See attachment !
      Attached Files

      Comment

      • reginaldmerritt
        New Member
        • Nov 2006
        • 201

        #4
        Mihail, thank you very much for your reply and attachment. I understand exactly what you are saying. That is most definitely a much better approach, a lot easier as well ;)

        Comment

        Working...