473,394 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

IIF() Statement in Query based on Option Value

reginaldmerritt
201 100+
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_____Text
Surname_______Text
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....(control 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!FRMStaffList.AssessorChosen

i.e.
Expand|Select|Wrap|Line Numbers
  1. SELECT TBStaff.Assessor, TBStaff.Trainer, TBStaff.IV
  2. FROM TBStaff
  3. WHERE (((TBStaff.Assessor)=[Forms]![FRMStaffList].[AssessorChosen]) AND 
  4.       ((TBStaff.Trainer)=[Forms]![FRMStaffList].[TrainerChosen]) AND 
  5.       ((TBStaff.IV)=[Forms]![FRMStaffList].[IVChosen]));
  6.  
However, if I do this for all employment types the query will only look for single employment types.

Consider example below:
Expand|Select|Wrap|Line Numbers
  1. Name           Assessor     Trainer    IV 
  2. Bill Hicks      True
  3. Bill Bailey     True         True
  4. Bill Gates      True                   True
  5. Bill Murry                   True
  6.  
If I wanted to show just Assessors the query will will evaluate as
Expand|Select|Wrap|Line Numbers
  1. SELECT TBStaff.Assessor, TBStaff.Trainer, TBStaff.IV
  2. FROM TBStaff
  3. WHERE (((TBStaff.Assessor)=TRUE) AND
  4.       ((TBStaff.Trainer)=FALSE]) AND 
  5.       ((TBStaff.IV)=FALSE));
  6.  
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.
Expand|Select|Wrap|Line Numbers
  1. SELECT TBStaffTmp.TBStaffID, TBStaffTmp.Forenames, TBStaffTmp.Assessor, TBStaffTmp.Trainer, TBStaffTmp.IV
  2. FROM TBStaffTmp
  3. WHERE (((TBStaffTmp.Assessor)=IIf([Forms]![FRMStaffListTMP].[AssessorChosen]=True,True,True Or False)) AND 
  4.       ((TBStaffTmp.Trainer)=IIf([Forms]![FRMStaffListTMP].[TrainerChosen]=True,True,True Or False)) AND 
  5.       ((TBStaffTmp.IV)=IIf([Forms]![FRMStaffListTMP].[IVChosen]=True,True,True Or False)));
  6.  


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.
Feb 28 '12 #1

✓ answered by Mihail

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:
http://bytes.com/topic/access/insigh...ble-structures

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

3 2400
reginaldmerritt
201 100+
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.
Expand|Select|Wrap|Line Numbers
  1. SELECT TBStaffTmp.StaffID, TBStaffTmp.Forenames, TBStaffTmp.Assessor, TBStaffTmp.Trainer, TBStaffTmp.IV
  2. FROM TBStaffTmp
  3. WHERE (((TBStaffTmp.Assessor)=[Forms]![FRMStaffListTMP].[AssessorChosen]));
  4.  
Then
Expand|Select|Wrap|Line Numbers
  1. SELECT TBStaffTmp.StaffID, TBStaffTmp.Forenames, TBStaffTmp.Assessor, TBStaffTmp.Trainer, TBStaffTmp.IV
  2. FROM TBStaffTmp
  3. WHERE (((TBStaffTmp.Trainer)=[Forms]![FRMStaffListTMP].[TrainerChosen]));
and
Expand|Select|Wrap|Line Numbers
  1. SELECT TBStaffTmp.StaffID, TBStaffTmp.Forenames, TBStaffTmp.Assessor, TBStaffTmp.Trainer, TBStaffTmp.IV
  2. FROM TBStaffTmp
  3. WHERE (((TBStaffTmp.IV)=[Forms]![FRMStaffListTMP].[IVChosen]));
  4.  
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
Expand|Select|Wrap|Line Numbers
  1. SELECT TBStaffTmp.TBStaffID, TBStaffTmp.Forenames, TBStaffTmp.Assessor, TBStaffTmp.Trainer, TBStaffTmp.IV
  2. FROM TBStaffTmp
  3. WHERE (((TBStaffTmp.Assessor)=FALSE));
  4.  
This would show record BILL MURRY as that record does have TBStaffTmp.Assessor 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.
Expand|Select|Wrap|Line Numbers
  1. SELECT TBStaffID, Forenames, Assessor, Trainer, IV
  2. FROM QYStaffListTMP1
  3. WHERE Assessor = True
  4. UNION ALL
  5. SELECT TBStaffID, Forenames, Assessor, Trainer, IV
  6. FROM QYStaffListTMP2
  7. WHERE Trainer = True
  8. UNION ALL SELECT TBStaffID, Forenames, Assessor, Trainer, IV
  9. FROM QYStaffListTMP3
  10. WHERE IV = True;
  11.  
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.
Feb 28 '12 #2
Mihail
759 512MB
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:
http://bytes.com/topic/access/insigh...ble-structures

Anyway, I sketch a normalized database for you and I do your job (in my way, of course)
See attachment !
Attached Files
File Type: zip StaffAbilities.zip (42.4 KB, 95 views)
Feb 29 '12 #3
reginaldmerritt
201 100+
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 ;)
Feb 29 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: JDJones | last post by:
Using PHP and MySQL. Trying to put a list of categories into a drop down select option of a form like: <form name="form" action="<? print $_SERVER?>" method="get"> <select name="subject">...
2
by: Piotr Wolski | last post by:
more or less that is my problem: i'm making HTML <select><option> statement: if ($dir = @opendir("/the/directory)) { while($file = readdir($dir)) { echo '<option value="'.$file.'">'.$file;...
5
by: RICHARD BROMBERG | last post by:
I am using MS Access 2000 and crating an application where the main table has about 90 fields. I understand how to run a where the value of a PARTICULAR field is entered into a text box. I want...
3
by: Stijn Vanroye | last post by:
Hi List, I'm running a query on a not-so-small db. Mostly this query runs fast enough, but every once in a while the query takes a long time to complete in wich case the users start banging away...
2
by: amith.srinivas | last post by:
Hi all, From a word macro in VBA, I am trying to create a report in access DB. The report is based on a query with a parameter. I am using Set rpt = Application.CreateReport rpt.RecordSource =...
6
by: Kermit Piper | last post by:
Hello, I thought this should be easy, but... all I want to do is set the value of this state drop-down based on a session var I'm getting back from a redirect (from the processing page): <%...
4
by: magix | last post by:
Hi, in my SELECT (like below), the option value has 2 values, separated with comma "," <select name="sIdName"> <option value="10,John">John</option> <option value="11,Eva">Eva</option>...
9
by: bubbles | last post by:
Newbie transiting from VBA to TSQL, using SQL Server 2005 Enterprise: Need help to do this: Open Table_A WITH TableA DO UNTIL .EOF Read value from TableA.ColumnA Run SQL Statement on...
4
by: Joe | last post by:
This is my HTML form: <form method=get action="home.php"> <INPUT TYPE = "Text" VALUE = "" NAME = "title"><br> <select name="searchby"> <option value="Title">Title</option> <option...
1
by: Karen D | last post by:
Help Again!! I’m using Access 2003 and I have a form that allows users to enter criteria for selecting tables and queries as well as the query parameters that will be used to generate a report. The...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.