473,433 Members | 2,051 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,433 software developers and data experts.

SQL Query with an IF/Else where statement

8
Need some help here...

I am working in Access and have a query that gets selection criteria from a form with 6 selection options. I also want to be able to check for "all: in any of the 6 combo box criteria selections. Below is a query that might work but am not sure how to implement or if it is correct.
Expand|Select|Wrap|Line Numbers
  1. SELECT tblContacts.CntSalutation, tblContacts.CntFirstName, tblContacts.CntLastName, tblContacts.CntMailAddr, tblContacts.CntStrAddr, tblContacts.CntCity, tblContacts.CntState, tblContacts.CntZip
  2. FROM tblContacts INNER JOIN tlnkOrgCategories ON tblContacts.CntID = tlnkOrgCategories.OrgID
  3.  
  4. IF (([forms]![frmreports].[cat1] OR[forms]![frmreports].[cat2] Or [forms]![frmreports].[cat3] Or [forms]![frmreports].[cat4] Or [forms]![frmreports].[cat5] Or [forms]![frmreports].[cat6])) = "All"
  5. THEN
  6. WHERE  (tlnkOrgCategories.OrgCategory) is not null
  7. Else
  8. WHERE
  9. (((tlnkOrgCategories.OrgCategory)=[forms]![frmreports].[cat1] 
  10. Or (tlnkOrgCategories.OrgCategory)=[forms]![frmreports].[cat2] 
  11. Or (tlnkOrgCategories.OrgCategory)=[forms]![frmreports].[cat3] 
  12. Or (tlnkOrgCategories.OrgCategory)=[forms]![frmreports].[cat4] 
  13. Or (tlnkOrgCategories.OrgCategory)=[forms]![frmreports].[cat5] 
  14. Or (tlnkOrgCategories.OrgCategory)=[forms]![frmreports].[cat6]))
  15. END IF
  16. GROUP BY tblContacts.CntSalutation, tblContacts.CntFirstName, tblContacts.CntLastName, tblContacts.CntMailAddr, tblContacts.CntStrAddr, tblContacts.CntCity, tblContacts.CntState, tblContacts.CntZip
  17. ORDER BY forms!frmreports.srt1; 
  18.  
I would appreciate info on how to implement, or is there a better way to select "all"?

Thanks!
PW
Aug 23 '07 #1
2 7745
JConsulting
603 Expert 512MB
Need some help here...

I am working in Access and have a query that gets selection criteria from a form with 6 selection options. I also want to be able to check for "all: in any of the 6 combo box criteria selections. Below is a query that might work but am not sure how to implement or if it is correct.

SELECT tblContacts.CntSalutation, tblContacts.CntFirstName, tblContacts.CntLastName, tblContacts.CntMailAddr, tblContacts.CntStrAddr, tblContacts.CntCity, tblContacts.CntState, tblContacts.CntZip
FROM tblContacts INNER JOIN tlnkOrgCategories ON tblContacts.CntID = tlnkOrgCategories.OrgID

IF (([forms]![frmreports].[cat1] OR[forms]![frmreports].[cat2] Or [forms]![frmreports].[cat3] Or [forms]![frmreports].[cat4] Or [forms]![frmreports].[cat5] Or [forms]![frmreports].[cat6])) = "All"
THEN
WHERE (tlnkOrgCategories.OrgCategory) is not null
Else
WHERE
(((tlnkOrgCategories.OrgCategory)=[forms]![frmreports].[cat1]
Or (tlnkOrgCategories.OrgCategory)=[forms]![frmreports].[cat2]
Or (tlnkOrgCategories.OrgCategory)=[forms]![frmreports].[cat3]
Or (tlnkOrgCategories.OrgCategory)=[forms]![frmreports].[cat4]
Or (tlnkOrgCategories.OrgCategory)=[forms]![frmreports].[cat5]
Or (tlnkOrgCategories.OrgCategory)=[forms]![frmreports].[cat6]))
END IF
GROUP BY tblContacts.CntSalutation, tblContacts.CntFirstName, tblContacts.CntLastName, tblContacts.CntMailAddr, tblContacts.CntStrAddr, tblContacts.CntCity, tblContacts.CntState, tblContacts.CntZip
ORDER BY forms!frmreports.srt1;

I would appreciate info on how to implement, or is there a better way to select "all"?

Thanks!
PW

Give this a try. SQL for queries in access use IIF

Expand|Select|Wrap|Line Numbers
  1. SELECT tblContacts.CntSalutation, tblContacts.CntFirstName, tblContacts.CntLastName, tblContacts.CntMailAddr, tblContacts.CntStrAddr, tblContacts.CntCity, tblContacts.CntState, tblContacts.CntZip
  2. FROM tblContacts INNER JOIN tlnkOrgCategories ON tblContacts.CntID = tlnkOrgCategories.OrgID
  3. WHERE IIF([forms]![frmreports].[cat1]="All" OR
  4. [forms]![frmreports].[cat2]="All"  Or 
  5. [forms]![frmreports].[cat3]="All"  Or 
  6. [forms]![frmreports].[cat4]="All"  Or 
  7. [forms]![frmreports].[cat5]="All"  Or 
  8. [forms]![frmreports].[cat6] = "All", tlnkOrgCategories.OrgCategory is not null,
  9. tlnkOrgCategories.OrgCategory=[forms]![frmreports].[cat1]
  10. Or (tlnkOrgCategories.OrgCategory)=[forms]![frmreports].[cat2]
  11. Or (tlnkOrgCategories.OrgCategory)=[forms]![frmreports].[cat3]
  12. Or (tlnkOrgCategories.OrgCategory)=[forms]![frmreports].[cat4]
  13. Or (tlnkOrgCategories.OrgCategory)=[forms]![frmreports].[cat5]
  14. Or (tlnkOrgCategories.OrgCategory)=[forms]![frmreports].[cat6])
  15. GROUP BY tblContacts.CntSalutation, tblContacts.CntFirstName, tblContacts.CntLastName, tblContacts.CntMailAddr, tblContacts.CntStrAddr, tblContacts.CntCity, tblContacts.CntState, tblContacts.CntZip
  16. ORDER BY forms!frmreports.srt1;
  17.  
Aug 24 '07 #2
paulw4
8
Got it working, I appreciate the help.
Aug 30 '07 #3

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

Similar topics

2
by: jaysonsch | last post by:
Hello! I am having some problems with a database query that I am trying to do. I am trying to develop a way to search a database for an entry and then edit the existing values. Upon submit, the...
8
by: Dave | last post by:
Hi all, I've been trying to figure this out for the last day and a half and it has me stumped. I've got a web application that I wrote that keeps track of trading cards I own, and I'm moving it...
9
by: wiredog | last post by:
I am struggling rewriting my query from MS Access' IIF, Then to SQL Servers TSQL language. I am hoping some one can give me some guidance. I believe I have the first portion of the query correct...
22
by: Robert Brown | last post by:
suppose I have the following table: CREATE TABLE (int level, color varchar, length int, width int, height int) It has the following rows 1, "RED", 8, 10, 12 2, NULL, NULL, NULL, 20...
1
by: Stefan V. | last post by:
Hello! I am trying to convert a query written for SQL Server 2000 database tables, to a MS Access query. Here is what I have in SQL Server: SELECT t2.*, CASE WHEN t2.QType = '3' THEN...
0
by: starace | last post by:
I have designed a form that has 5 different list boxes where the selections within each are used as criteria in building a dynamic query. Some boxes are set for multiple selections but these list...
0
by: JC Voon | last post by:
Hi All: I'm new in Threading and Web Services, can someone please verify my code, i'm not sure whether this is the correct way, althought it is partially work, but some time it will raise...
16
by: Sam Durai | last post by:
Hello, I need to merge a small table (of rows less than 100,sometimes even 0 rows) to a big table (of rows around 4 billion). I used the PK of the big table as merge key but merge does a table scan...
1
by: tomlebold | last post by:
Having problems displaying query results from combo boxes on a sub form, which is on the same form that is used to select criteria. This has always worked form me when displaying query results on...
0
by: RCapps | last post by:
When running the below SQL Query I keep getting the following error: Server: Msg 4924, Level 16, State 1, Line 1 ALTER TABLE DROP COLUMN failed because column 'ContractDef' does not exist in table...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.