Solved

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

Posted on 2011-03-15
9
346 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
  • 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 119

Accepted Solution

by:
Rey Obrero earned 500 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 51

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
 
LVL 51

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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

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 119

Expert Comment

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

Author Comment

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

Expert Comment

by:Rey Obrero
ID: 35140197
did you try the post at http:#a35138571
0
 
LVL 51

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 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

17 Experts available now in Live!

Get 1:1 Help Now