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

Using AND & OR Operators in SQL query Criteria to make choices

Jerry Maiapu
259 100+
Hello Everyone,
I have 3 combos on a form (budled with- year, quarter and month)that users will use them as filters to run a report.
What I want is to use the OR & AND operators in the query as the record source to generate report based on values selected on the 3 combos. I also have the 3 corresponding columns names in my query.
Any ideas in way forward is appreciated.

This is a snap shot query of what am using:

1st attempt:

Expand|Select|Wrap|Line Numbers
  1. Select  fld_year, fld_qrter, fld_month,
  2. From Table1
  3. Where fld_year=[year field from form] OR  fld_ qrter =[quarter field from form] OR fld_month=[month field from form];
2nd Attmenpt-using iif:

Expand|Select|Wrap|Line Numbers
  1. Select  fld_year, fld_qrter, fld_month,
  2. From Table1
  3. Where iif(isnull([quarter field from form] fld_year=[year field from form],iif(isnull([month field from form]), fld_ qrter =[quarter field from form], fld_month=[month field from form]));
Note: On the form, the year combo field is required whilst the three months is automatically populated after quarter is selected from the quarter combo;

What I want to achieve is:
(1)if only year is selected in the combo box then the query should filter records for year only else
(2)if Year and Quarter selected then display for that quarter in that year only else
(3) if Month is selected then display query results for that month of the year only


Thanks..
Jan 7 '13 #1

✓ answered by Anas Mosaad

Your first attempt will return results in any year or quarter or month selected. Here's a snippet that hopefully solves your problem.
Expand|Select|Wrap|Line Numbers
  1. where 
  2. fld_year=[year field from form] AND
  3. (
  4. (isnull([month field from form]) and isnull([quarter field from form])
  5. OR 
  6. (isnull([month field from form]) and fld_ qrter =[quarter field from form])
  7. OR
  8. (fld_month=[month field from form]) and fld_ qrter =[quarter field from form])
  9. )
  10.  
I'm not sure Using IIF could fit in this case. Hopefully the above solution is enough.

7 1372
Anas Mosaad
185 128KB
Your first attempt will return results in any year or quarter or month selected. Here's a snippet that hopefully solves your problem.
Expand|Select|Wrap|Line Numbers
  1. where 
  2. fld_year=[year field from form] AND
  3. (
  4. (isnull([month field from form]) and isnull([quarter field from form])
  5. OR 
  6. (isnull([month field from form]) and fld_ qrter =[quarter field from form])
  7. OR
  8. (fld_month=[month field from form]) and fld_ qrter =[quarter field from form])
  9. )
  10.  
I'm not sure Using IIF could fit in this case. Hopefully the above solution is enough.
Jan 7 '13 #2
NeoPa
32,556 Expert Mod 16PB
The first thing I suggest you do Jerry, is to handle the form such that when either B or C has a selection, the other is disabled. When a selection is cleared this must also trigger re-enabling the other of course. Also, from your 1,2,3 explanation (though not from the rest unfortunately), it seems you also need the Year value specified in all circumstances. If that is true, then use then take similar steps to disable and re-enable both B and C depending on A.

Once you have your form constructed correctly you can then run the code that will build up your SQL string. It will always include :
Expand|Select|Wrap|Line Numbers
  1. strWhere = "([fld_Year]=#%A#)"
Where %A is determined by calling :
Expand|Select|Wrap|Line Numbers
  1. Format(CDate([Year Field from Form], "m\/d\/yyyy")
If either of the other two controls has a value then you need to append the following string to strWhere :
Expand|Select|Wrap|Line Numbers
  1. strWhere = strWhere & " AND ([fld_ Qrter]=[Quarter Field from Form])"
This example shows the Quarter format. Only one should ever be added to the string.

Let us know how you get on with this. If you have trouble with it please post what you've tried and what the issues were (in full and clear detail) and we'll help you further from there.
Jan 7 '13 #3
Jerry Maiapu
259 100+
Thanks Anas Mosaad and NeoPa both for your responses. In fact I combined part of both of your suggestions.

I have handled the form data entry using NeoPa's approach and used Anas Mosaad suggestion to pull filtered data off the query running the report.

NeoPa, I have also on the other hand tried your suggestion and worked as well so thanks Neo for your continues assistance.

Since am using Anas Mosaad suggestion..I just decided to choose his post as the best answer..if there was an option to choose two posts as best answers then I would have done so..

Thanks again both...
Jan 15 '13 #4
NeoPa
32,556 Expert Mod 16PB
You're very welcome Jerry. Don't worry about the Best Answer. I don't feel remotely disappointed. I get plenty anyway and understand as well as anyone that only one is available. It makes more sense to encourage a new poster than to worry about an old hand like me.
Jan 16 '13 #5
Anas Mosaad
185 128KB
@Jerry: I'm glad that you have a working solution.

@NeoPa: that a great spirit that is rare nowadays though I'm not caring about how much best answer I get and honestly I'm not tracking. I believe it doesn't make a difference as long as the person shows that your reply helped - even a bit. I'm trying to help people on my own and I'm not aiming to get rewarded for this. My only reward is that I participated in resolving someone's issue.
Jan 17 '13 #6
NeoPa
32,556 Expert Mod 16PB
Anas:
I believe it doesn't make a difference as long as the person shows that your reply helped - even a bit
Amen to that brother!

None of the rest hurts, but a polite OP makes all the difference for me. Not that it's absolutely necessary for my enjoyment, as I know all these threads are viewed by hundreds, and sometimes many more, of others with similar problems. Knowing I've helped is my enjoyment.
Jan 17 '13 #7
Anas Mosaad
185 128KB
I agree with your point Adrian, thanks.
Jan 17 '13 #8

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

Similar topics

7
by: keliie | last post by:
Hello Just a quick question that I've spent a few hours trying to solve with no luck (although one would think this should be fairly easy). I have a form with a subform. The subform is based...
4
by: TD | last post by:
I have a form that has a frame control named fraComplaint. I have a query that I need to return records that have an empty Date_Closed field when the value of fraComplaint is 2 and all records...
3
by: stevecat | last post by:
Hi there, I have created a form, "search" and a query "search_product". The query returns the product information based upon the criteria for three of the fields, author, title or isbn. The field...
3
by: ericargent | last post by:
Hi I'm using Acces 2003 I have Query where the several parameters for the criteria are supplied from a form. One parameter source is a combo box. What I am trying to do is if: An item is...
6
craigfr
by: craigfr | last post by:
I am making a graph comparing last year's defect data with YTD defect data. Our fiscal year starts Nov.1 and ends Oct.31. To get the YTD, I started used a simple date serial criteria: Between...
3
by: sfrvn | last post by:
I have searched high and low and cannot find an answer to my problem. So now I turn to the collective genius of this newsgroup. Over-simplified examples This query criteria for field works:...
2
by: cmartin1986 | last post by:
First of all I want to thank all of you that have helped me in the past this is an awesome fourm. My problem today is I have a database that builds charts that are viewed by a large group every...
8
by: limperger | last post by:
Hello everyone! First and foremost, my apologies for the title of the post. It is not very clarifying of what the problem is about, but I didn't know how to put it... My problem is as follows: I...
9
ajhayes
by: ajhayes | last post by:
Hello everyone, This is my first time posting here and I'm hoping someone can help me out. I'm a relative newbie to Access and am pretty much learning as I go along, so please bear with me. ...
5
by: hbaf208 | last post by:
I have an option group (TimeFrameOption) with three options: All, Last 6 months, and Last 12 Months. When an option is selected, I want to query dates in a listbox (CallReport) to display all dates...
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?
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.