473,418 Members | 4,802 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,418 software developers and data experts.

Build a filter query with AND and OR statement

41
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 3140
Rabbit
12,516 Expert Mod 8TB
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
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 Expert Mod 8TB
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
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
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 Expert Mod 8TB
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
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 Expert Mod 8TB
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
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)...
8
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...
2
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...
4
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...
16
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...
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...
3
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"...
1
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...
9
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
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...
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
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
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...

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.