Solved

Select fields (with Join) based on field values

Posted on 2014-10-02
7
454 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
Comment Utility
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
Comment Utility
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 31

Expert Comment

by:awking00
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 74

Expert Comment

by:Jeffrey Coachman
Comment Utility
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
Comment Utility
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 31

Expert Comment

by:awking00
Comment Utility
Did you try the switch function?
0
 
LVL 34

Expert Comment

by:PatHartman
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 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