Select fields (with Join) based on field values

Posted on 2014-10-02
Medium Priority
Last Modified: 2014-10-02
Hopefully I can explain this properly.

I have a service scheduling form that we want to try to use a "units" field on to (hopefully) more efficiently schedule crews.

Table 1- Work Orders - Fields:
W.WorkType (Install, Upgrade, Repair)

Table 2- Locations
L.InstallUnits (Value could be different at different locations)
L.UpgradeUnits (Value could be different at different locations)
L.RepairUnits (Value could be different at different locations)

The Form / Report needs to display
Location, WorkType, Units (ONCE, based on work type), Crew

The logic would be something like:
SELECT W.LocationID, W.WorkType, (If W.WorkType = Upgrade Then SELECT L.UpgradeUnits ELSEIF W.WorkType = Install Then SELECT L.InstallUnits ELSEIF W.WorkType=Repair THEN SELECT L.RepairUnits) AS Units FROM WorkOrders W INNER JOIN Locations L ON L.LocationID = W.LocationID

Problem is, I'm not sure of the Access compatible SQL statements to use (or if its even possible).  Specifically, in the stuff in bold - hopefully that's clear enough what I need, but the exact syntax that will produce the result in Access is what I need (alternatively, I MAY be able to convert this into a SQL view - the backend is SQL, but the data currently (without Units) is an Access Query using the backend SQL tables.
Question by:Lee W, MVP
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
LVL 24

Accepted Solution

Phillip Burton earned 1600 total points
ID: 40357171
Not 100% sure what you are asking, but if it is the code for this (If W.WorkType = Upgrade Then SELECT L.UpgradeUnits ELSEIF W.WorkType = Install Then SELECT L.InstallUnits ELSEIF W.WorkType=Repair THEN SELECT L.RepairUnits) in MS Access, then:

IIf(W.WorkType = "Upgrade", L.UpgradeUnits, iif(W.WorkType = "Install",L.InstallUnits, iif(W.WorkType="Repair", L.RepairUnits,"Error"))) 

Open in new window

LVL 38

Assisted Solution

PatHartman earned 400 total points
ID: 40357186
Access SQL uses the IIf() function to add logic to queries.  T-SQL (and many other variants use the CASE statement)

SELECT W.LocationID, W.WorkType, IIf(W.WorkType = "Upgrade", L.UpgradeUnits, IIf(W.WorkType = "Install", L.InstallUnits, L.RepairUnits)) AS Units FROM WorkOrders W INNER JOIN Locations L ON L.LocationID = W.LocationID

I made RepairUnits the default.  You don't need the third condition unless you actually have a fourth option and that is the default.
LVL 32

Expert Comment

ID: 40357323
Try the switch function in Access -
SELECT W.LocationID, W.WorkType,
switch(W.WorkType="Upgrade",L.UpgradeUnits,W.WorkType="install",L.InstallUnits,W.WorkType="Repair",L.RepairUnits) as WorkUnits
FROM WorkOrders W INNER JOIN Locations L ON L.LocationID = W.LocationID
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 40357429
Or, ...(If I am understanding your requirement correctly), ...you could also do this in code with something roughly like this.

Private Sub cboWorkType_AfterUpdate()
Dim strWorkType As String
Dim strSQL As String
    strWorkType = Me.cboWorkType
    strSQL = "SELECT W.LocationID, W.WorkType, L." & strWorkType & "Units AS UNITS FROM WorkOrders W INNER JOIN Locations L ON L.LocationID = W.LocationID"
    Me.txtSQL = strSQL
End Sub

...in this way no "If" logic is required...
Using If logic would mean that for each edit, addition or deletion of a WorkType, ...you would have to remember to manually edit the SQL.
...and also worry about getting the nesting parenthesis just right for each new (or deleted) Worktype

The code approach would simply insert the WorkType into the SQL string...

simple example attached

LVL 96

Author Closing Comment

by:Lee W, MVP
ID: 40357558
Thanks guys, that worked... Might revisit this later (know if there's a depth limit to nested IIFs?  I could potentially need a dozen or so...)
LVL 32

Expert Comment

ID: 40357682
Did you try the switch function?
LVL 38

Expert Comment

ID: 40357890
I'm not sure what the depth limit is to the IIf() but the human mind is definitely limited.  I wouldn't do more than 4.  If you need more than that, the Switch function will ultimately be easier to read.  Or, if you are running the query from a form, Jeff's idea might clean it up.

However, a database redesign would be better still.  You have a repeating group and repeating groups violate first normal form and cause substantial extra coding to use.  If the "buckets" are mutually exclusive, all you need is a type and a value.  If multiple "buckets" are valid, then you need a child table with a many to 1 relationship to the table you currently have.

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

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…
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

765 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