Solved

Query returning records it shuold not be returning

Posted on 2013-11-16
4
212 Views
Last Modified: 2013-11-16
I have a query with a table as its record source.  The are two fields in the query that I am setting a criteria for.  The are "Email" (text field), and "AssociationID" (numeric).

I don't want the query to return the records that have no number in "AssociationID" and have no text in "Email".  

But the query IS only returning the record that have a number in the "AssociationID" field but IS ALSO returning the records if the "Email" field is blank and it should not be.

Here is the SQL of the query:

SELECT tblAssociations.AssocName, [LastName] & ", " & [FirstName] AS Name, tblIndividuals.Email, tblIndividuals.AssociationID, tblIndividuals.FirstName, tblIndividuals.LastName
FROM tblIndividuals RIGHT JOIN tblAssociations ON tblIndividuals.AssociationID = tblAssociations.AssociationID
WHERE (((tblIndividuals.Email) Is Not Null) AND ((tblIndividuals.AssociationID) Is Not Null))
ORDER BY tblAssociations.AssocName, [LastName] & ", " & [FirstName];
0
Comment
Question by:SteveL13
  • 3
4 Comments
 
LVL 40

Accepted Solution

by:
Sharath earned 250 total points
ID: 39653372
try this.
SELECT tblAssociations.AssocName, [LastName] & ", " & [FirstName] AS Name, 
       tblIndividuals.Email, 
	   tblIndividuals.AssociationID, 
	   tblIndividuals.FirstName, 
	   tblIndividuals.LastName
  FROM tblIndividuals 
 RIGHT JOIN tblAssociations 
    ON tblIndividuals.AssociationID = tblAssociations.AssociationID
 WHERE (((tblIndividuals.Email) Is Not Null)
   AND trim(tblIndividuals.Email) <> '' 
   AND ((tblIndividuals.AssociationID) Is Not Null))
 ORDER BY tblAssociations.AssocName, [LastName] & ", " & [FirstName];

Open in new window

0
 
LVL 61

Assisted Solution

by:mbizup
mbizup earned 250 total points
ID: 39653396
Or this:

SELECT a.AssocName, [LastName] & ", " & [FirstName] AS Name, i.Email, i.AssociationID, i.FirstName, i.LastName
FROM tblAssociations a LEFT JOIN tblIndividuals i ON a.AssociationID = i.AssociationID
WHERE NZ(i.Email,"") <> ""  AND  NZ(i.AssociationID ,"") <> "" 
ORDER BY a.AssocName, [LastName] & ", " & [FirstName];

Open in new window

0
 
LVL 61

Expert Comment

by:mbizup
ID: 39653400
Also, if you're not allowing NULLs in associationID (ie: un-matched records), you can use an INNER JOIN without the associationID criteria:

SELECT a.AssocName, [LastName] & ", " & [FirstName] AS Name, i.Email, i.AssociationID, i.FirstName, i.LastName
FROM tblAssociations a INNER JOIN tblIndividuals i ON a.AssociationID = i.AssociationID
WHERE NZ(i.Email,"") <> "" 
ORDER BY a.AssocName, [LastName] & ", " & [FirstName];

Open in new window

0
 
LVL 61

Expert Comment

by:mbizup
ID: 39653442
Steve,

These all should give you the results you're looking for, but I'd recommend actually using the INNER JOIN in my last post (http:#a39653400).  That right join from what I can tell is overkill, and might (possibly) be problematic if you are using this as a recordsource for a form whose data you want to update.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now