Solved

How would you join another table to an existing query?

Posted on 2011-02-25
7
301 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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 to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…

747 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

12 Experts available now in Live!

Get 1:1 Help Now