Solved

Access:Union query: Where a field meets a condition not working

Posted on 2007-11-15
5
195 Views
Last Modified: 2010-03-20
I have a form called "Students". On this form is a textbox called "txtStudent". Also on this form is a listbox called "StudentList". I also have two tables ("Table1"and "Table2") both tables have the following fields ("StudentName", "SS#", "Grade","Status")

When someone types a name in the textbox ("txtStudent") and presses enter it runs the code below:

[Forms]![Students]![StudentList].RowSource = "SELECT [StudentName], [SS#], [Grade], [Status] FROM TABLE1 UNION SELECT [StudentName], [SS#], [Grade], [Status] FROM TABLE2 WHERE [StudentName] =[Forms]![Students]![txtStudent] ORDER BY [StudentName];"

This code is supposed to display in the list box only those records with a "StudentName"  equal to what the user typed in the textbox ("xtStudent"). Nonetheless the code displays all records anyway. Will someone help me make the code work?
0
Comment
Question by:ouestque
  • 2
5 Comments
 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
ID: 20293541
Give this a try:

[Forms]![Students]![StudentList].RowSource = "SELECT * FROM (SELECT [StudentName], [SS#], [Grade], [Status] FROM TABLE1 UNION SELECT [StudentName], [SS#], [Grade], [Status] FROM TABLE2 ) WHERE [StudentName] =[Forms]![Students]![txtStudent] ORDER BY [StudentName]"
0
 
LVL 7

Expert Comment

by:lappins
ID: 20297271
The first side of your UNION has no where clause, hence is returning all rows. Try this:

Forms]![Students]![StudentList].RowSource = "SELECT [StudentName], [SS#], [Grade], [Status] FROM TABLE1 WHERE [StudentName] =[Forms]![Students]![txtStudent] UNION SELECT [StudentName], [SS#], [Grade], [Status] FROM TABLE2 WHERE [StudentName] =[Forms]![Students]![txtStudent] ORDER BY [StudentName];"
0
 

Author Comment

by:ouestque
ID: 20314145
MBzip got the answer first so she gets the points. Thanks you both!!!
0
 
LVL 61

Expert Comment

by:mbizup
ID: 20315612
Glad to help out!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

706 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

18 Experts available now in Live!

Get 1:1 Help Now