473,804 Members | 3,049 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Build a filter query with AND and OR statement

41 New Member
Hello,

I am trying to build a filter query with some AND and OR.

I have three text boxes and 5 check boxes. The checkboxes are linked via code to other textboxes for the purpose of the query.

The first three text boxes are:
[CompName]
[AOName]
[GroupName]

The other five text boxes are:
[WillNot]
[Contacted]
[Promised]
[Opened]
[NotContacted]

They are all on a form and the result form on which is applied the filter query is on a sub form.

I want the user to be able to filter the data with [CompName], [AOName] & [GroupName] being all linked by AND (one excludes the other).

Then, in the meantime, the user will tick some of the five check boxes, which will activate some of the text boxes (they are not visible to the user), and all this should be OR linked (we can have some data which answers two or more of these criteria).

If there are no matching records, the query will return no results (or at best have a msgbox(“No results found”)).

My query is as follows:

Expand|Select|Wrap|Line Numbers
  1. SELECT tblProspects.CompName, tblProspects.Status, tblProspects.AOName, tblProspects.GroupName, tblProspects.InputDate
  2. FROM tblProspects
  3. WHERE (((tblProspects.CompName)=[Forms]![frmMgtMain]![CompName]) AND
  4. ((tblProspects.AOName)=[Forms]![frmMgtMain]![AOName]) AND
  5.  ((tblProspects.GroupName)=[Forms]![frmMgtMain]![GroupName]) AND
  6.  ((tblProspects.Status)=[Forms]![frmMgtMain]![WillNot] Or
  7.  (tblProspects.Status)=[Forms]![frmMgtMain]![Contacted] Or
  8.  (tblProspects.Status)=[Forms]![frmMgtMain]![Promised] Or
  9.  (tblProspects.Status)=[Forms]![frmMgtMain]![Opened] Or
  10.  (tblProspects.Status)=[Forms]![frmMgtMain]![NotContacted]))
  11. GROUP BY tblProspects.CompName, tblProspects.Status, tblProspects.AOName, tblProspects.GroupName, tblProspects.InputDate;
But it does not work the way I want: the OR criteria are recognized and work well, whereas it does not even take into account the AND criteria.

Can someone help?

Thank you very much and best regards.

G.
Aug 28 '07 #1
8 3171
Rabbit
12,516 Recognized Expert Moderator MVP
With AND, the problem comes in when the user leaves it blank, i.e. NULL. It won't match up on any field. You have to account for it by using Like Variable & "*". However, this brings in the problem of pulling in similar records which you may not want.
Aug 28 '07 #2
Gari
41 New Member
Hello,

Thank you for the reply. Actually I already use the "*" as a default value for all the text boxes. The problem with my AND statements is that it is simply not working. Nothing happens, whereas for the OR stuff, it works dramatically well. Would it be of any use to build a nested query?
Aug 29 '07 #3
Rabbit
12,516 Recognized Expert Moderator MVP
There's nothing wrong that I can see with the code. What do you mean by it doesn't work?

Can you provide some sample records, values in the text boxes, and the results that you get as opposed to the results that you should get?
Aug 29 '07 #4
Gari
41 New Member
The behaviour of the program is like that:

1. When you open the page, nothing is selected and all text boxes contain the "*" star. We thus expect all the data to appear. However, result is blank. Nothing at all appears.

2. We can play with whatever criteria from [CompName], [AOName] & [GroupName] (the AND filter), no results at all are returned. For instance if we select a company name, still no results will appear.

3. We can play with the criteria [WillNot], [Contacted], [Promised], [Opened], [NotContacted] (the OR statement), and thus effectively the query returns the results containing the criteria we have selected.

4. We cannot filter again the result of 3. using [CompName], [AOName] & [GroupName].

5. When a criteria from 3. is deselected, I don't know for what reason, but 3 results are still appearing with their status being a star "*" (like if the program had replaced their status with *).

I may attach a simple version of my db if you wish.

Thank you for your attention and best regards.

G.
(btw, I just double checked the names and they are correct)
Aug 29 '07 #5
Gari
41 New Member
Here are some examples:

Let’s say I have those data (I did not take into account [CompName] and [GroupName] as the behaviour would be the same as [AOName]):

[John Doe] [Not Contacted]
[Emeveret Dupit] [Contacted]
[Owen Solep] [Promised]
[John Doe] [Will Not]
[Emeveret Dupit] [Opened]
[John Doe] [Not Contacted]
[Emeveret Dupit] [Contacted]
[Owen Solep] [Promised]
[John Doe] [Will Not]
[Emeveret Dupit] [Opened]
[Owen Solep] [Not Contacted]
[John Doe] [Contacted]
[Emeveret Dupit] [Promised]
[Owen Solep] [Will Not]
[John Doe] [Opened]
[Emeveret Dupit] [Not Contacted]
[Owen Solep] [Contacted]
[John Doe] [Promised]
[Emeveret Dupit] [Will Not]
[Owen Solep] [Opened]

1. First, When I open the page, all the criteria are at “*”, so I should see everything. However, no results are returned.

2. Let’s say I tick the [Not Contacted] and [Contacted] checkboxes. I get the following:

[John Doe] [Not Contacted]
[Emeveret Dupit] [Contacted]
[John Doe] [Not Contacted]
[Emeveret Dupit] [Contacted]
[Owen Solep] [Not Contacted]
[John Doe] [Contacted]
[Emeveret Dupit] [Not Contacted]
[Owen Solep] [Contacted]

3. Now I want to filter again to see only the customers of John Doe. I should get:

[John Doe] [Not Contacted]
[John Doe] [Not Contacted]
[John Doe] [Contacted]

Whereas actually I get:

[John Doe] [Not Contacted]
[Emeveret Dupit] [Contacted]
[John Doe] [Not Contacted]
[Emeveret Dupit] [Contacted]
[Owen Solep] [Not Contacted]
[John Doe] [Contacted]
[Emeveret Dupit] [Not Contacted]
[Owen Solep] [Contacted]

Means: the filter is not applying.

4. Now I deselect [Not Contacted] and [Contacted], I get

[John Doe] [*]
[Emeveret Dupit] [*]
[Owen Solep] [*]

Three values that do not move anymore, with their status replaced by a star *. Which actually is not the expected behaviour (I should go back to the initial list).

5. If then I check again the check box [Not Contacted], I would get:

[John Doe] [*]
[Emeveret Dupit] [*]
[Owen Solep] [*]
[John Doe] [Not Contacted]
[Owen Solep] [Not Contacted]
[Emeveret Dupit] [Not Contacted]

Whereas I should get:

[John Doe] [Not Contacted]
[John Doe] [Not Contacted]
[Owen Solep] [Not Contacted]
[Emeveret Dupit] [Not Contacted]

Hope this was explicit.

Best regards,

G.
Aug 29 '07 #6
Rabbit
12,516 Recognized Expert Moderator MVP
The behaviour of the program is like that:

1. When you open the page, nothing is selected and all text boxes contain the "*" star. We thus expect all the data to appear. However, result is blank. Nothing at all appears.
This is because you're not using Like. You're using = in your SQL code.

2. We can play with whatever criteria from [CompName], [AOName] & [GroupName] (the AND filter), no results at all are returned. For instance if we select a company name, still no results will appear.
Again this is most likely because you're using = instead of Like.

3. We can play with the criteria [WillNot], [Contacted], [Promised], [Opened], [NotContacted] (the OR statement), and thus effectively the query returns the results containing the criteria we have selected.
I don't know how this could work unless you took the CompName, AOName, and GroupName out of the SQL statement. What is WillNot, Contacted, Promised, Opened, and NotContacted? It sounds like they're checkboxes while the field is a string or coded numer, in which case I can't see how this would work.

4. We cannot filter again the result of 3. using [CompName], [AOName] & [GroupName].
This is probably because you the Like versus = issue.

5. When a criteria from 3. is deselected, I don't know for what reason, but 3 results are still appearing with their status being a star "*" (like if the program had replaced their status with *).
I have no idea about this one, maybe you have some records in there with * as their value.
Aug 29 '07 #7
Gari
41 New Member
I have worked out this:

Expand|Select|Wrap|Line Numbers
  1. SELECT tblProspects.CompName, tblProspects.Status, tblProspects.AOName, tblProspects.GroupName, tblProspects.InputDate
  2. FROM tblProspects
  3. WHERE (((tblProspects.CompName) Like [Forms]![frmMgtMain]![CompName]) AND ((tblProspects.AOName) Like [Forms]![frmMgtMain]![AOName]) AND ((tblProspects.GroupName) Like [Forms]![frmMgtMain]![GroupName]) AND ((tblProspects.Status)=[Forms]![frmMgtMain]![WillNot])) OR (((tblProspects.CompName) Like [Forms]![frmMgtMain]![CompName]) AND ((tblProspects.AOName) Like [Forms]![frmMgtMain]![AOName]) AND ((tblProspects.GroupName) Like [Forms]![frmMgtMain]![GroupName]) AND ((tblProspects.Status)=[Forms]![frmMgtMain]![NotContacted])) OR (((tblProspects.CompName) Like [Forms]![frmMgtMain]![CompName]) AND ((tblProspects.AOName) Like [Forms]![frmMgtMain]![AOName]) AND ((tblProspects.GroupName) Like [Forms]![frmMgtMain]![GroupName]) AND ((tblProspects.Status)=[Forms]![frmMgtMain]![Opened])) OR (((tblProspects.CompName) Like [Forms]![frmMgtMain]![CompName]) AND ((tblProspects.AOName) Like [Forms]![frmMgtMain]![AOName]) AND ((tblProspects.GroupName) Like [Forms]![frmMgtMain]![GroupName]) AND ((tblProspects.Status)=[Forms]![frmMgtMain]![Promised])) OR (((tblProspects.CompName) Like [Forms]![frmMgtMain]![CompName]) AND ((tblProspects.AOName) Like [Forms]![frmMgtMain]![AOName]) AND ((tblProspects.GroupName) Like [Forms]![frmMgtMain]![GroupName]) AND ((tblProspects.Status)=[Forms]![frmMgtMain]![Contacted]))
  4. GROUP BY tblProspects.CompName, tblProspects.Status, tblProspects.AOName, tblProspects.GroupName, tblProspects.InputDate;
Which seems to work like I want.

Thank you for your time and best regards.

G.
Aug 30 '07 #8
Rabbit
12,516 Recognized Expert Moderator MVP
Glad you got it working. Good luck.
Aug 30 '07 #9

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

Similar topics

4
3250
by: Phil Powell | last post by:
http://www.php.net/array_filter I went there at first for my information on filtering mySQL query results using PHP, to no avail. This is more of a Vignette construct (my native environment) so bear with me. I decided the easiest course of action to tackle a perplexing problem involving two queries and having to
8
6536
by: dick | last post by:
I am just trying to print/report the results of a "filter by selection" which is done by right-clicking a form, filling in values, and "applying the filter." I have searched the newsgroups, and there are many examples. BUT, they fail sometimes. The techique is to pass the form's Me.filter as the "where condition" in a Docmd.openreport statement in code behind a "print button" on the form.
2
4409
by: Seth Delaney | last post by:
I have a form with multiple unbound text boxes which serves as a "search form". I can enter my search parameters in the various boxes as needed and click okay. My records are then filtered to produce the results I want in a separate form (filter by form). No problem, except I want to save the sql to a query, not a form. When I debug.print the various sql, I get something like this example: (( LIKE "cam*")) which obviously is not enough...
4
5140
by: Nhmiller | last post by:
This is directly from Access' Help: "About designing a query When you open a query in Design view, or open a form, report, or datasheet and show the Advanced Filter/Sort window (Advanced Filter/Sort window: A window in which you can create a filter from scratch. You enter criteria expressions in the filter design grid to restrict the records in the open form or datasheet to a subset of records that meet the criteria.), you see the design...
16
18425
by: Nhmiller | last post by:
I already have a report designed under Reports. When I use filtering in Forms or Tables, I see no way to select that filtered list when I am in Reports, and there appears to be no way to do the filtering when I am in Reports. I have received two replies to this message, posted a few days ago within another Subject. One requires making programming type statements. That should not be necessary -- Access must have a quick way to just click...
0
3518
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 boxes do not necessarily need to have a selection made to be used in the dynamic query. In essence the form can have selections made in all or none of its list boxes to form the dynamic query I am looking to get some feedback in reference to...
3
2689
by: Brian Birtle | last post by:
**** A CHALLENGE TO THE GURUS - refute the statement "It's impossible to build a file upload progress meter using ASP.NET" **** First person to prove me wrong gets "All Time .NET Programming GOD" listing in my address book and (optionally) their name listed on my "news" page of my birtle.com website (listed as "Jane Smith is a Programming GOD") for at least a month. Why not take a moment to read more and possibly boost your ego to all time...
1
16392
by: lorirobn | last post by:
Hi, I have a report that works just fine. Now I would like to add the capability to choose selection criteria to limit what is displayed. I created several reports that do this, but they used VBA to create SQL statements, then apply them. Now that I have been using Access/VBA for awhile, I am wondering if there is a simpler way to do this than coding these long sql statements. My report has a query as its record source. Can I just...
9
6097
by: dee | last post by:
I'd like to filter by the following criteria: left(LeadDisposition,3) = "Sit" AND Appt_Date = Text767 I have no idea how to do this. Appreciate help.
0
9704
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10562
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10303
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10070
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7608
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6845
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5508
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4282
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.