473,320 Members | 1,965 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,320 software developers and data experts.

Access query to include records WITHOUT a certain value

Try as I might I cannot find a way to write an access query to return a
result set with the records from my database WITHOUT a certain set of
values within a field.

To explain, I have a table of customers linked to a table of customer
contacts. The contact table has a field called 'type of contact'.
Contact types can be numeric 1 to 40 and show the different types of
contact I have with my customers (e.g. 1 - initial contact, 2 -
follow-up, 3 - service call, 4 - customer query etc etc).

All the contacts are recorded in the contact table, so a customer will
have multiple entries in the contact table.

I want a list of customers WITHOUT contact values 30 through 35 to be
shown. However just asking for <30 and >35, returns all customers with
values outside this criteria, i.e. customers with contact types 1-29
and 36-40.

I want to know how to return a list of customers which don't have
contact types 30-35 within the contact type table.

Very many thanks for your help.

Jon

Sep 11 '06 #1
5 2524
could you: first write a query to find all contacts with contact type
30 - 35 and then use the results fo that query as an 'excluding query'
to select all contacts except those in the excluding query?

Alternatively, can you not use OR in the QBE form, that is put "<30 or
>35" in contact type 1, then put "<30 or >35" on the next 'OR' line for contact type 2, etc, etc.
DG.

Sep 11 '06 #2
redstamp wrote:
Try as I might I cannot find a way to write an access query to return a
result set with the records from my database WITHOUT a certain set of
values within a field.

To explain, I have a table of customers linked to a table of customer
contacts. The contact table has a field called 'type of contact'.
Contact types can be numeric 1 to 40 and show the different types of
contact I have with my customers (e.g. 1 - initial contact, 2 -
follow-up, 3 - service call, 4 - customer query etc etc).

All the contacts are recorded in the contact table, so a customer will
have multiple entries in the contact table.

I want a list of customers WITHOUT contact values 30 through 35 to be
shown. However just asking for <30 and >35, returns all customers with
values outside this criteria, i.e. customers with contact types 1-29
and 36-40.

I want to know how to return a list of customers which don't have
contact types 30-35 within the contact type table.

Very many thanks for your help.

Jon
(Untested)

SELECT *
FROM CONTACTS
WHERE CONTACTS.CUSTID NOT IN (
SELECT C2.CUSTID FROM CONTACTS AS C2
WHERE C2.CTYPE BETWEEN 30 AND 35)
;

--
Smartin
Sep 11 '06 #3

Smartin wrote:
redstamp wrote:
Try as I might I cannot find a way to write an access query to return a
result set with the records from my database WITHOUT a certain set of
values within a field.

To explain, I have a table of customers linked to a table of customer
contacts. The contact table has a field called 'type of contact'.
Contact types can be numeric 1 to 40 and show the different types of
contact I have with my customers (e.g. 1 - initial contact, 2 -
follow-up, 3 - service call, 4 - customer query etc etc).

All the contacts are recorded in the contact table, so a customer will
have multiple entries in the contact table.

I want a list of customers WITHOUT contact values 30 through 35 to be
shown. However just asking for <30 and >35, returns all customers with
values outside this criteria, i.e. customers with contact types 1-29
and 36-40.

I want to know how to return a list of customers which don't have
contact types 30-35 within the contact type table.

Very many thanks for your help.

Jon

(Untested)

SELECT *
FROM CONTACTS
WHERE CONTACTS.CUSTID NOT IN (
SELECT C2.CUSTID FROM CONTACTS AS C2
WHERE C2.CTYPE BETWEEN 30 AND 35)
;

--
Smartin
Thanks for that Smartin, just wondered, what this was trying to do, do
I can insert names where appropriate, i.e. what is C2 and don't I need
to pull * from Customer, not Contacts?

Cheers in advance for your help.

Jon.

Sep 12 '06 #4
redstamp wrote:
Smartin wrote:
>redstamp wrote:
>>Try as I might I cannot find a way to write an access query to return a
result set with the records from my database WITHOUT a certain set of
values within a field.

To explain, I have a table of customers linked to a table of customer
contacts. The contact table has a field called 'type of contact'.
Contact types can be numeric 1 to 40 and show the different types of
contact I have with my customers (e.g. 1 - initial contact, 2 -
follow-up, 3 - service call, 4 - customer query etc etc).

All the contacts are recorded in the contact table, so a customer will
have multiple entries in the contact table.

I want a list of customers WITHOUT contact values 30 through 35 to be
shown. However just asking for <30 and >35, returns all customers with
values outside this criteria, i.e. customers with contact types 1-29
and 36-40.

I want to know how to return a list of customers which don't have
contact types 30-35 within the contact type table.

Very many thanks for your help.

Jon
(Untested)

SELECT *
FROM CONTACTS
WHERE CONTACTS.CUSTID NOT IN (
SELECT C2.CUSTID FROM CONTACTS AS C2
WHERE C2.CTYPE BETWEEN 30 AND 35)
;

--
Smartin

Thanks for that Smartin, just wondered, what this was trying to do, do
I can insert names where appropriate, i.e. what is C2 and don't I need
to pull * from Customer, not Contacts?

Cheers in advance for your help.

Jon.
My bad, I was returning CONTACTS records, you wanted CUSTOMERS.

SELECT *
FROM CUSTOMERS
WHERE CUSTOMERS.CUSTID NOT IN (
SELECT C2.CUSTID FROM CONTACTS AS C2
WHERE C2.CTYPE BETWEEN 30 AND 35)
;
what this was trying to do?
As suggested by another poster, the technique to find "What X are not in
Y" is sometimes solved by taking an inverse approach: Determine what X
/are in/ Y, and exclude those from the results.

In this case, the subquery builds a list of CONTACTS who have at least
one contact type between 30 and 35. The outer query says, give me all
the customers that do NOT appear in the subquery.

This sort of thing can also be done using JOINs. I find the subquery to
be easier to read, though IIRC the JOIN construct is more efficient. In
a small table (a few 1000's of rows) my guess is it won't make any
noticeable difference.
What is C2?
C2 is an alias for the CONTACTS table. The alias accomplishes two
things. One, since I chose a very short name it makes it a little easier
to refer to CONTACTS fields. More importantly in the query I posted
first the alias distinguishes between two instances of the CONTACTS
table. For this reason the alias would be required in the first query.
It is optional in the second.

Hope this helps!

--
Smartin
Sep 12 '06 #5
Thank you very much, this was most helpful indeed!
Smartin wrote:
redstamp wrote:
Smartin wrote:
redstamp wrote:
Try as I might I cannot find a way to write an access query to return a
result set with the records from my database WITHOUT a certain set of
values within a field.

To explain, I have a table of customers linked to a table of customer
contacts. The contact table has a field called 'type of contact'.
Contact types can be numeric 1 to 40 and show the different types of
contact I have with my customers (e.g. 1 - initial contact, 2 -
follow-up, 3 - service call, 4 - customer query etc etc).

All the contacts are recorded in the contact table, so a customer will
have multiple entries in the contact table.

I want a list of customers WITHOUT contact values 30 through 35 to be
shown. However just asking for <30 and >35, returns all customers with
values outside this criteria, i.e. customers with contact types 1-29
and 36-40.

I want to know how to return a list of customers which don't have
contact types 30-35 within the contact type table.

Very many thanks for your help.

Jon

(Untested)

SELECT *
FROM CONTACTS
WHERE CONTACTS.CUSTID NOT IN (
SELECT C2.CUSTID FROM CONTACTS AS C2
WHERE C2.CTYPE BETWEEN 30 AND 35)
;

--
Smartin
Thanks for that Smartin, just wondered, what this was trying to do, do
I can insert names where appropriate, i.e. what is C2 and don't I need
to pull * from Customer, not Contacts?

Cheers in advance for your help.

Jon.

My bad, I was returning CONTACTS records, you wanted CUSTOMERS.

SELECT *
FROM CUSTOMERS
WHERE CUSTOMERS.CUSTID NOT IN (
SELECT C2.CUSTID FROM CONTACTS AS C2
WHERE C2.CTYPE BETWEEN 30 AND 35)
;
what this was trying to do?

As suggested by another poster, the technique to find "What X are not in
Y" is sometimes solved by taking an inverse approach: Determine what X
/are in/ Y, and exclude those from the results.

In this case, the subquery builds a list of CONTACTS who have at least
one contact type between 30 and 35. The outer query says, give me all
the customers that do NOT appear in the subquery.

This sort of thing can also be done using JOINs. I find the subquery to
be easier to read, though IIRC the JOIN construct is more efficient. In
a small table (a few 1000's of rows) my guess is it won't make any
noticeable difference.
What is C2?

C2 is an alias for the CONTACTS table. The alias accomplishes two
things. One, since I chose a very short name it makes it a little easier
to refer to CONTACTS fields. More importantly in the query I posted
first the alias distinguishes between two instances of the CONTACTS
table. For this reason the alias would be required in the first query.
It is optional in the second.

Hope this helps!

--
Smartin
Sep 14 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: | last post by:
If I need to check if a certain value does exist in a field, and return either "yes" or "not" which query would be the most effestive?
4
by: Brian | last post by:
I hope this will make sense. I'm trying to filter the records in a table based on records in a 2nd table. The trick is, I can't use a query. I'm trying to filter down the number of records going...
1
by: Sreeneet | last post by:
Hi all, I want to call a stored procedure which is written in SQL Server from an ms-access query. It is having some parameters also and the stored procedure will return some records. Is there...
3
by: Simon | last post by:
Dear reader, I found out a strange behaviour in a query of the type Total (summation query). In case of a normal select query with a criteria setting Is Null for field-A, four (4) records...
4
by: jkuruvil | last post by:
I need to print a report of a bunch of records that falls between certain dates. Some records have dates and some don't. I did a UNION query to look for everything that falls between these two dates...
1
by: mrkselm | last post by:
Hi, I am stuck with a problem in MS Access which does not occur in SQL Server and I have been banging my head against the wall for a couple of days now trying to resolve it. Namely, when I...
3
by: austin1539 | last post by:
I am trying to run an Access query from Excel. The query, called "ProdActs1" works in Access and is run from using information from a table called "Queries". When a button is clicked in Excel, data...
2
by: Comandur | last post by:
Hi, I am trying to export an access query to excel. I have made use of transferspreadsheet command to achive this. However i have hardcoded the path and the filename in the VBA code. I am not sure...
6
by: jsacrey | last post by:
Hey everybody, got a secnario for ya that I need a bit of help with. Access 97 using linked tables from an SQL Server 2000 machine. I've created a simple query using two tables joined by one...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.