Solved

How can I do a SQL query?

Posted on 2003-12-09
18
235 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

739 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