Click here to Skip to main content
15,893,190 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to join 2 tables and display in a datagrid. I am using Access database and SQL statement. I get a "Syntax error in FROM clause". I can't figure out why.

Access database, 2 tables "tblMeter" and "tblOwner".

What I have tried:

This is what I have.

.CommandText = "SELECT tblMeter.MeterNo, tblMeter.MeterCusAcctNo, tblMeter.MeterOwnerID" &
                                  " FROM tblMeter" &
                                  " JOIN tblOwner" &
                                  " WHERE tblMeter.MeterOwnerID = tblOwner.OwnerID;"


It is probably simple but it alludes me. Thanks in advance.
Posted
Updated 30-Jan-20 3:25am
Comments
F-ES Sitecore 30-Jan-20 9:26am    
Look up the syntax for "JOIN", try to specify the fields that match in the join clause rather than the where.

Try to replace WHERE with ON and see what happens :)
Access SQL: basic concepts, vocabulary, and syntax - Access[^]
 
Share this answer
 
Comments
Maciej Los 30-Jan-20 13:32pm    
5!
Use ON for the join conditions, not WHERE.
VB.NET
.CommandText = "SELECT tblMeter.MeterNo, tblMeter.MeterCusAcctNo, tblMeter.MeterOwnerID" &
               " FROM tblMeter" &
               " JOIN tblOwner" &
               " ON tblMeter.MeterOwnerID = tblOwner.OwnerID;"
Perform joins using Access SQL | Microsoft Docs[^]
 
Share this answer
 
Comments
bgcwaterman 30-Jan-20 9:40am    
Thank for all the help. I replaced the WHERE to ON. Still didn't work. I Read the article I noticed they were using INNER JOIN and not just JOIN. So I Replaced the JOIN and it got me past the Error. Now I will have to get it to the Datagrid.
Maciej Los 30-Jan-20 13:38pm    
"Now I will have to get it to the Datagrid." - This is another story...
Please, accept Richard's solution (use green button), because it answers your question.
All you need to do now is to use OleDbDataReader Class[^] together with DataTable.Load Method[^]. Then:
DataGridView.DataSource = DataTable1

That's all!
Maciej Los 30-Jan-20 13:32pm    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900