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
Solved

How would you join another table to an existing query?

Posted on 2011-02-25
7
328 Views
Last Modified: 2012-05-11
I am developing and Access 2003 application using an MDB type file

How would you join another table named tblBanksRev to the follwing query as follows?

SELECT tblBanks.*, tblDates.dtRec, tblOpenItems.*
FROM tblDates,
tblBanks INNER JOIN tblOpenItems ON tblBanks.[Bank Code]=tblOpenItems.Bank
WHERE tblOpenItems.t In ("A","D","E");

ADD the following table to the Query:

tblBanksRev.[BRS #]  = tblBanks.[Bank Code]
0
Comment
Question by:zimmer9
7 Comments
 
LVL 5

Expert Comment

by:MikkelAStrojek
ID: 34982319
SELECT tblBanks.*, tblDates.dtRec, tblOpenItems.*
FROM tblDates, tblBanks
INNER JOIN tblOpenItems ON tblBanks.[Bank Code]=tblOpenItems.Bank
INNER JOIN tblBanksRev ON tblBanksRev.[Bank Code]=tblBanks .BankWHERE tblOpenItems.t In ("A","D","E");

The bold highlighted depends on what the tables have in common

0
 
LVL 4

Expert Comment

by:Alex Matzinger
ID: 34982333
Like this
SELECT tblBanks.*, tblDates.dtRec, tblOpenItems.*
FROM tblDates,  
tblBanks 
INNER JOIN tblOpenItems ON tblBanks.[Bank Code]=tblOpenItems.Bank
INNER JOIN tblBanksRev ON tblBanksRev.[BRS #]  = tblBanks.[Bank Code]
WHERE tblOpenItems.t In ("A","D","E");

Open in new window

0
 
LVL 6

Expert Comment

by:AkAlan
ID: 34982344
SELECT tblBanks.*, tblDates.dtRec, tblOpenItems.*
FROM tblDates,
tblBanks
INNER JOIN tblOpenItems ON tblBanks.[Bank Code] = tblOpenItems.Bank
INNER JOIN tblBanksRev ON tblBanksRev.[BRS #]  = tblBanks.[Bank Code]
WHERE tblOpenItems.t In ("A","D","E");

This will only return records if there are records in tblBanksRev where the [BRS #] from tblBanksRev  matches [Bank Code] tblBanks

If you want All the records from tblBanks and any that have related records in tblBanksRev then you need to join it with a LEFT OUTER JOIN like this


SELECT tblBanks.*, tblDates.dtRec, tblOpenItems.*
FROM tblDates,
tblBanks
INNER JOIN tblOpenItems ON tblBanks.[Bank Code] = tblOpenItems.Bank
LEFT OUTER JOIN tblBanksRev ON tblBanksRev.[BRS #]  = tblBanks.[Bank Code]
WHERE tblOpenItems.t In ("A","D","E");

0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

Author Comment

by:zimmer9
ID: 34982988
SELECT tblBanks.*, tblDates.dtRec, tblOpenItems.*
FROM tblDates,
tblBanks
INNER JOIN tblOpenItems ON tblBanks.[Bank Code] = tblOpenItems.Bank
LEFT OUTER JOIN tblBanksRev ON tblBanksRev.[BRS #]  = tblBanks.[Bank Code]
WHERE tblOpenItems.t In ("A","D","E");

The above statement causes the following error:

Syntax error (missing operator) in query expression
'tblBanks.[Bank Code] = tblOpenItems.Bank
Left outer join tblBanksRev.[BRS #] = tblBanks.[Bank Code]'.

However, if I leave out the following LEFT OUTER JOIN statement from the query, then it works fine:

LEFT OUTER JOIN tblBanksRev ON tblBanksRev.[BRS #]  = tblBanks.[Bank Code]
0
 

Author Comment

by:zimmer9
ID: 34983090
Let me restate the error:

Syntax error (missing operator) in query expression
'tblBanks.[Bank Code] = tblOpenItems.Bank
LEFT OUTER JOIN tblBanksRev on
tblBanksRev.[BRS #] = tblBanks.[Bank Code]'.

0
 

Author Comment

by:zimmer9
ID: 34983165
If I use the following query:

SELECT tblBanks.*, tblDates.dtRec, tblOpenItems.*
FROM tblDates,  
tblBanks
INNER JOIN tblOpenItems ON tblBanks.[Bank Code]=tblOpenItems.Bank
INNER JOIN tblBanksRev ON tblBanksRev.[BRS #]  = tblBanks.[Bank Code]
WHERE tblOpenItems.t In ("A","D","E");

then the error is very similar as follows:

Syntax error (missing operator) in query expression
'tblBanks.[Bank Code] = tblOpenItems.Bank
INNER JOIN tblBanksRev ON
tblBanksRev.[BRS #] = tblBanks.[Bank Code]'.



0
 
LVL 6

Accepted Solution

by:
AkAlan earned 500 total points
ID: 34983964
Your statement is definately not correct. You are trying to select all records from tblBanks, one column from tblDates (dtRec) and all from tblOpenItems

you then have FROM tblDates,tblBanks. That is a problem, you don't seperate the tables with a comma, you do it with joins
So you need a join with tblBanks

SELECT tblBanks.*, tblDates.dtRec, tblOpenItems.*

FROM tblDates

INNER JOIN tblBanks ON tblBanks.[SomeKeyGoesHere] = tblDates.[SomeKeyGoesHere]
INNER JOIN tblOpenItems ON tblBanks.[Bank Code]=tblOpenItems.Bank
INNER JOIN tblBanksRev ON tblBanksRev.[BRS #]  = tblBanks.[Bank Code]
WHERE tblOpenItems.t In ("A","D","E");

Without knowing the table schema, there is no way for me to know which keys to join on.

I think you really need to study up on joins to understand the concept. http://www.w3schools.com/sql/default.asp has great free tutorials.




0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

766 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