Solved

Query returning records it shuold not be returning

Posted on 2013-11-16
4
223 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

920 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

11 Experts available now in Live!

Get 1:1 Help Now