Solved

Select fields (with Join) based on field values

Posted on 2014-10-02
7
457 Views
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.LocationID
W.WorkType (Install, Upgrade, Repair)
W.Crew

Table 2- Locations
L.LocationID
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.
0
Comment
Question by:Lee W, MVP
7 Comments
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 400 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

0
 
LVL 34

Assisted Solution

by:PatHartman
PatHartman earned 100 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.
0
 
LVL 32

Expert Comment

by:awking00
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
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

JeffCoachman
Database35.mdb
0
 
LVL 95

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...)
0
 
LVL 32

Expert Comment

by:awking00
ID: 40357682
Did you try the switch function?
0
 
LVL 34

Expert Comment

by:PatHartman
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.
0

Featured Post

3 Use Cases for Connected Systems

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

Question has a verified solution.

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

Suggested Solutions

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

863 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

21 Experts available now in Live!

Get 1:1 Help Now