Solved

How can I do a SQL query?

Posted on 2003-12-09
18
234 Views
Last Modified: 2011-09-20
Hi Experts,
I have problem retrieving data out of my database. Using access, I work on a Visual basic school project assignment. I have 3 tables. stock_no is the primary key in my inventory table. a_stock_no is a field in another table which matches primary key. and i_stock_no is another field in a different table which also matches primary key. I'm looking for a SQL query to display one record at a time. I tried this: "SELECT * FROM TABLE1,TABLE2,TABLE3 WHERE TABLE1.STOCK_NO = TABLE2.A_STOCK_NO AND TABLE1.STOCK_NO = TABLE3.I_STOCK_NO" I got an error saying: EOF or BOF empty or something like that. then I tried to change AND to OR. it worked but not correctly. it displayed each record twice. first matched with A_STOCK_NO then matched with I_STOCK_NO. hope I'm clear. thanks for any help.
0
Comment
Question by:schesht
  • 9
  • 7
18 Comments
 
LVL 9

Accepted Solution

by:
p_sie earned 50 total points
ID: 9906072
How about

SELECT DISTINCT TABLE1.* FROM TABLE1 INNER JOIN TABLE2 ON TABLE1.STOCK_NO = TABLE2.A_STOCK_NO INNER JOIN TABLE3 ON TABLE1.STOCK_NO = TABLE3.I_STOCK_NO

Distinct means that only distinct values are displayed
When using * with multiple tables always mention the tablename in front!!

Good luck
0
 

Author Comment

by:schesht
ID: 9906248
Hi p_sie,
I'm getting a syntax error saying: missing operator in query expression 'TABLE1.STOCK_NO = TABLE2.A_STOCK_NO INNER JOIN TABLE3 ON TABLE1.STOCK_NO = TABLE3.I_STOCK_NO'
0
 
LVL 9

Expert Comment

by:p_sie
ID: 9906479
What's your complete query?
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:schesht
ID: 9906570
i've tried this it kinda worked for me the way i wanted but there's an issue. evey time i want to go to the next record i have to click next button twice. the query i tried is as follows:
"SELECT DISTINCT * FROM TABLE1,TABLE2,TABLE3 WHERE TABLE1.STOCK_NO = TABLE2.A_STOCK_NO OR TABLE1.STOCK_NO = TABLE3.I_STOCK_NO OR TABLE1.PRODUCT = 'For Sale' ORDER BY TABLE1.STOCK_NO"
i dont know why i have to click two times.
0
 
LVL 9

Expert Comment

by:p_sie
ID: 9906761
What do you see when you run the query in Access?
0
 

Author Comment

by:schesht
ID: 9907062
in access when i run it i see nothing but field names.
0
 
LVL 9

Expert Comment

by:p_sie
ID: 9907111
That would mean that no records match the query and what about the query i proposed above
what does that return in access?
0
 

Author Comment

by:schesht
ID: 9907415
i think i said I'm getting a syntax error with your query-  saying: missing operator in query expression 'TABLE1.STOCK_NO = TABLE2.A_STOCK_NO INNER JOIN TABLE3 ON TABLE1.STOCK_NO = TABLE3.I_STOCK_NO' . and the full query is like:
SELECT DISTINCT TABLE1.* FROM TABLE1 INNER JOIN TABLE2 ON TABLE1.STOCK_NO = TABLE2.A_STOCK_NO INNER JOIN TABLE3 ON TABLE1.STOCK_NO = TABLE3.I_STOCK_NO
0
 

Author Comment

by:schesht
ID: 9908167
Ok,
I ran the following query in access the outcome displayed as each record i mean same record twice for the matching criteria. is there any way i can fix it.
0
 

Author Comment

by:schesht
ID: 9908173
"SELECT DISTINCT * FROM TABLE1,TABLE2,TABLE3 WHERE TABLE1.STOCK_NO = TABLE2.A_STOCK_NO OR TABLE1.STOCK_NO = TABLE3.I_STOCK_NO OR TABLE1.PRODUCT = 'For Sale' ORDER BY TABLE1.STOCK_NO"
0
 
LVL 9

Expert Comment

by:p_sie
ID: 9910512
There has got to be a field in the double results that is not the same as you have DISTINCT in your query, this always filters out the double
results.
Try to explicitely type the fields you want to query, like:

SELECT DISTINCT TABLE1.Stock_no, TABLE2.A_STOCK_NO FROM .....

0
 

Author Comment

by:schesht
ID: 9910528
Hi experts,
any hope so far?
0
 

Author Comment

by:schesht
ID: 9910556
the redundancy actually depends on the number of the records i have in my database in all three tables. what i mean is that i entered 4 records the out come was four times now. in other words, if i have 100 records in my tables 2 and 100 records in my table 3 it displays 200 records. the number of records in table2 plus the number of records in table3. I hope i'm clear by all this. cause i'm confused myself.
0
 
LVL 9

Expert Comment

by:p_sie
ID: 9910612
That's because you have the following:

WHERE TABLE1.STOCK_NO = TABLE2.A_STOCK_NO OR TABLE1.STOCK_NO = TABLE3.I_STOCK_NO

A record matches your criteria if it's Table1.Stock_no equals table2.a_stock_no OR table3.i_stock_no
SO the record is displayed twice because you use SELECT * FROM , this way all posibilities are displyed
Like I said before, try using INNER JOIN's OR maybe OUTER JOIN's , Try making the query you want in access and then
right click in the area and select SQL , you will see the SQL-query as it is correct!!
0
 

Author Comment

by:schesht
ID: 9914311
Hello p_sie,
do you think you can check my access file if by any way I can send it to you. that way you may come up to alternative way to do things around. I would really appreciate that.
0
 
LVL 9

Expert Comment

by:p_sie
ID: 9918747
I will have a look at it if you put it on the internet somewhere, just let me know where to get it.

Greetings, p_sie
0
 
LVL 27

Expert Comment

by:planocz
ID: 10234871
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
No response from schesht from 12/11/2003 comment
Award points to p_sie is recommend.
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

planocz
EE Cleanup Volunteer
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

828 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