Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do I join 4 Tables using VB6 and MS Access using SQL?

Posted on 2011-03-15
9
Medium Priority
?
354 Views
Last Modified: 2012-05-11
I have 4 tables; Shifts, Employees, Locations, and Employee Types.

My Goal is to Select a group of shifts that fall within a date criteria and join all of the above tables. Here's what I came up with but it is obviously not working or I wouldn't be posting here :-(

Select * From Shifts LEFT JOIN Employees ON Employees.EmployeeID=Shifts.EmployeeID LEFT JOIN Location ON Location.LocationID=Shifts.LocationID LEFT JOIN EmployeeTypes ON EmployeeTypes.TypeID=Employees.EmployeeType Where Shifts.Begin < #16-Mar-2011 12:00:00 AM# And Shifts.End > #15-Mar-2011 12:00:00 AM# And (Shifts.Type = 2 OR Shifts.Type = 3)

The Location Table, may or may not have an entry in the table that corresponds with the Shifts.LocationID (ie: I want to support a location ID of 0 in the shifts table)

Some help with this SQL would really be appreciated!



0
Comment
Question by:DymaxionDeveloper
[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
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 77

Expert Comment

by:peter57r
ID: 35138484
I can't see anything wrong in principle with the query, but where multiple joins are involved, Access requires lots of ( ) .
You would be better off creating the same query in the Access query designer and then copying the sql from that.
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 2000 total points
ID: 35138571
try this

SELECT Shifts.*
FROM ((Shifts LEFT JOIN Employees ON Shifts.EmployeeID = Employees.EmployeeID) LEFT JOIN Location ON Shifts.LocationID = Location.LocationID) LEFT JOIN EmployeeTypes ON Employees.EmployeeType = EmployeeTypes.TypeID
Where Shifts.Begin < #16-Mar-2011 12:00:00 AM# And Shifts.End > #15-Mar-2011 12:00:00 AM# And (Shifts.Type = 2 OR Shifts.Type = 3)
0
 
LVL 60

Expert Comment

by:HainKurt
ID: 35139067
do you get any data for thisa

Select *
From
Shifts LEFT JOIN Employees ON Employees.EmployeeID=Shifts.EmployeeID
LEFT JOIN Location ON Location.LocationID=Shifts.LocationID
LEFT JOIN EmployeeTypes ON EmployeeTypes.TypeID=Employees.EmployeeType
Where (Shifts.Type = 2 OR Shifts.Type = 3)

0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 60

Expert Comment

by:HainKurt
ID: 35139141
try this too

Select * 
  From (select * 
          from Shifts 
         where Shifts.Begin < #16-Mar-2011 12:00:00 AM# And Shifts.End > #15-Mar-2011 12:00:00 AM# And (Shifts.Type = 2 OR Shifts.Type = 3)
       ) as s 
       LEFT JOIN Employees as e ON e.EmployeeID=s.EmployeeID
       LEFT JOIN Location as l ON l.LocationID=s.LocationID 
       LEFT JOIN EmployeeTypes as et ON et.TypeID=e.EmployeeType

Open in new window

0
 

Author Comment

by:DymaxionDeveloper
ID: 35139481
This does not work, there error I get is: Syntax Error (Missing Operator) in Query expression <SQL>

Error number 3075
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 35139541
which one is not working?
0
 

Author Comment

by:DymaxionDeveloper
ID: 35140157
Both responded by HainKurt
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 35140197
did you try the post at http:#a35138571
0
 
LVL 60

Expert Comment

by:HainKurt
ID: 35140464
put some (..)
Select * 
  From (((select * 
          from Shifts 
         where Shifts.Begin < #16-Mar-2011 12:00:00 AM# And Shifts.End > #15-Mar-2011 12:00:00 AM# And (Shifts.Type = 2 OR Shifts.Type = 3)
       ) as s 
       LEFT JOIN Employees as e ON e.EmployeeID=s.EmployeeID) 
LEFT JOIN Location as l ON l.LocationID=s.LocationID)
LEFT JOIN EmployeeTypes as et ON et.TypeID=e.EmployeeType

Open in new window

0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

604 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