Advertisement

04.02.2008 at 12:06AM PDT, ID: 23288289
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.0

How to Convert a SELECT query with IIF statement into transact SQL for an ADP Form

Asked by lin100 in MS SQL Server, SQL Query Syntax, Microsoft ADP

How to Convert SELECT query with IIF statement into transact SQL for an ADP Form.
Access 2002 and SQL 2000 Server.

I have an Access database named Supply.MDB
I have two combo boxes (Dept, and SO) on a form.
I also have a SELECT query named Q_FilteringQuery with IIF statement showed below.

SELECT [1_Job - Parent].Department_Name, [1_Job - Parent].SONumber
FROM [1_Job - Parent]
WHERE (([1_Job-Parent].Department_Name=IIf([Forms]![Selector]![Dept]  
                IsNull,[Department_Name],[Forms]![Selector]![Dept])) AND
([1_Job - Parent].SONumber=IIf([Forms]![Selector]![so] Is Null,[sonumber],[Forms]![Selector]![so])));

/////////////////////////////////////////////////////

Combo Box Name: Dept
Row Source: SELECT DISTINCT Q_FilteringQuery.Department_Name FROM Q_FilteringQuery ORDER BY Q_FilteringQuery.Department_Name;

Combo Box Name: So
Row Source: SELECT DISTINCT Q_FilteringQuery.SONumber FROM Q_FilteringQuery ORDER BY Q_FilteringQuery.SONumber;

/////////////////////////////////////////////////////

Private Sub Dept_AfterUpdate()
Me.Dept.Requery
Me.so.Requery
End Sub

Private Sub SO_AfterUpdate()
Me.Dept.Requery
Me.so.Requery
End Sub

/////////////////////////////////////////////////////

Everything from above works fine.

Now. When I take the above SELECT query that has IIF in the select statement, and converted to
transact SQL to be used in the ADP form, I get some very long VBA code. It works without bugs
but it is very long. Imagine if there are 4 combo boxes instead of 2, the code would be even longer!!

Question: Is there anyway to put code within the transact SQL to mimic the IIF statement ?
There are four different scenarios that Dept and SO combo box can have and that is why the transact SQL is so long

Dept                                     SO
Custom (Not Null)                 ______ (Is Null)
Custom (Not Null)                13056 (Not Null)
______ (Is Null)                   13056 (Not Null)
______ (Is Null)                   ______ (Is Null)

*******************************************************

Private Sub Dept_AfterUpdate()
Dim strSQL As String
Dim SQL_SO As String
Dim LgSO As Long
Dim strDept As String

If Not IsNull(Me.Dept) Then
strDept = CStr(Me.Dept)
End If

If Not IsNull(Me.so) Then
LgSO = CLng(Me.so)
End If

If Not IsNull(Me.Dept) And IsNull(Me.so) Then
strSQL = "SELECT DISTINCT [1_Job - Parent].Department_Name"
strSQL = strSQL & " FROM dbo.[1_Job - Parent] "
strSQL = strSQL & " WHERE dbo.[1_Job - Parent].Department_Name = '" & strDept & "'"
strSQL = strSQL & " ORDER BY Department_Name"
Me.Dept.RowSource = strSQL

SQL_SO = "SELECT DISTINCT [1_Job - Parent].SONumber"
SQL_SO = SQL_SO & " FROM dbo.[1_Job - Parent] "
SQL_SO = SQL_SO & " WHERE dbo.[1_Job - Parent].Department_Name = '" & strDept & "'"
SQL_SO = SQL_SO & " ORDER BY SONumber"
Me.so.RowSource = SQL_SO
Else
If Not IsNull(Me.Dept) And Not IsNull(Me.so) Then
strSQL = "SELECT DISTINCT [1_Job - Parent].Department_Name"
strSQL = strSQL & " FROM dbo.[1_Job - Parent] "
strSQL = strSQL & " WHERE dbo.[1_Job - Parent].Department_Name = '" & strDept & "'"
strSQL = strSQL & " AND SONumber =" & LgSO
strSQL = strSQL & " ORDER BY Department_Name"
Me.Dept.RowSource = strSQL

SQL_SO = "SELECT DISTINCT [1_Job - Parent].SONumber"
SQL_SO = SQL_SO & " FROM dbo.[1_Job - Parent] "
SQL_SO = SQL_SO & " WHERE dbo.[1_Job - Parent].Department_Name = '" & strDept & "'"
SQL_SO = SQL_SO & " AND SONumber =" & LgSO
SQL_SO = SQL_SO & " ORDER BY SONumber"
Me.so.RowSource = SQL_SO
Else
If IsNull(Me.Dept) And Not IsNull(Me.so) Then
strSQL = "SELECT DISTINCT [1_Job - Parent].Department_Name"
strSQL = strSQL & " FROM dbo.[1_Job - Parent] "
strSQL = strSQL & " WHERE dbo.[1_Job - Parent].SONumber = " & LgSO
strSQL = strSQL & " ORDER BY Department_Name"
Me.Dept.RowSource = strSQL

SQL_SO = "SELECT DISTINCT [1_Job - Parent].SONumber"
SQL_SO = SQL_SO & " FROM dbo.[1_Job - Parent] "
SQL_SO = SQL_SO & " WHERE dbo.[1_Job - Parent].SONumber = " & LgSO
SQL_SO = SQL_SO & " ORDER BY SONumber"
Me.so.RowSource = SQL_SO
Else
If IsNull(Me.Dept) And IsNull(Me.so) Then
strSQL = "SELECT DISTINCT [1_Job - Parent].Department_Name"
strSQL = strSQL & " FROM dbo.[1_Job - Parent] "
strSQL = strSQL & " ORDER BY Department_Name"
Me.Dept.RowSource = strSQL

SQL_SO = "SELECT DISTINCT [1_Job - Parent].SONumber"
SQL_SO = SQL_SO & " FROM dbo.[1_Job - Parent] "
SQL_SO = SQL_SO & " ORDER BY SONumber"
Me.so.RowSource = SQL_SO
End If
End If
End If
End If
End Sub

*******************************************************

Private Sub SO_AfterUpdate()
Dim SQL_Dept As String
Dim strSQL As String
Dim LgSO As Long
Dim strDept As String

If Not IsNull(Me.Dept) Then
strDept = CStr(Me.Dept)
End If

If Not IsNull((Me.so)) Then
LgSO = CLng(Me.so)
End If

If Not IsNull(Me.Dept) And IsNull(Me.so) Then
strSQL = "SELECT DISTINCT [1_Job - Parent].SONumber"
strSQL = strSQL & " FROM dbo.[1_Job - Parent] "
strSQL = strSQL & " WHERE dbo.[1_Job - Parent].Department_Name = '" & strDept & "'"
strSQL = strSQL & " ORDER BY SONumber"
Me.so.RowSource = strSQL

SQL_Dept = "SELECT DISTINCT [1_Job - Parent].Department_Name"
SQL_Dept = SQL_Dept & " FROM dbo.[1_Job - Parent] "
SQL_Dept = SQL_Dept & " WHERE dbo.[1_Job - Parent].Department_Name = '" & strDept & "'"
SQL_Dept = SQL_Dept & " ORDER BY Department_Name"
Me.Dept.RowSource = SQL_Dept
Else
If Not IsNull(Me.Dept) And Not IsNull(Me.so) Then
strSQL = "SELECT DISTINCT [1_Job - Parent].SONumber"
strSQL = strSQL & " FROM dbo.[1_Job - Parent] "
strSQL = strSQL & " WHERE dbo.[1_Job - Parent].Department_Name = '" & strDept & "'"
strSQL = strSQL & " AND SONumber =" & LgSO
strSQL = strSQL & " ORDER BY SONumber"
Me.so.RowSource = strSQL

SQL_Dept = "SELECT DISTINCT [1_Job - Parent].Department_Name"
SQL_Dept = SQL_Dept & " FROM dbo.[1_Job - Parent] "
SQL_Dept = SQL_Dept & " WHERE dbo.[1_Job - Parent].Department_Name = '" & strDept & "'"
SQL_Dept = SQL_Dept & " AND SONumber =" & LgSO
SQL_Dept = SQL_Dept & " ORDER BY Department_Name"
Me.Dept.RowSource = SQL_Dept
Else
If IsNull(Me.Dept) And Not IsNull(Me.so) Then
strSQL = "SELECT DISTINCT [1_Job - Parent].SONumber"
strSQL = strSQL & " FROM dbo.[1_Job - Parent] "
strSQL = strSQL & " WHERE dbo.[1_Job - Parent].SONumber = " & LgSO
strSQL = strSQL & " ORDER BY SONumber"
Me.so.RowSource = strSQL

SQL_Dept = "SELECT DISTINCT [1_Job - Parent].Department_Name"
SQL_Dept = SQL_Dept & " FROM dbo.[1_Job - Parent] "
SQL_Dept = SQL_Dept & " WHERE dbo.[1_Job - Parent].SONumber = " & LgSO
SQL_Dept = SQL_Dept & " ORDER BY Department_Name"
Me.Dept.RowSource = SQL_Dept
Else
If IsNull(Me.Dept) And IsNull(Me.so) Then
strSQL = "SELECT DISTINCT [1_Job - Parent].SONumber"
strSQL = strSQL & " FROM dbo.[1_Job - Parent] "
strSQL = strSQL & " ORDER BY SONumber"
Me.so.RowSource = strSQL

SQL_Dept = "SELECT DISTINCT [1_Job - Parent].Department_Name"
SQL_Dept = SQL_Dept & " FROM dbo.[1_Job - Parent] "
SQL_Dept = SQL_Dept & " ORDER BY Department_Name"
Me.Dept.RowSource = SQL_Dept
End If
End If
End If
End If
End Sub

*******************************************************
Known Issue:
      The alternative is to use Stored Procedure which I already have done and it works.
      I just wondered if there is anyway to mimic IIF within a SELECT transact SQLStart Free Trial
[+][-]04.02.2008 at 12:21AM PDT, ID: 21260734

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.02.2008 at 12:22AM PDT, ID: 21260737

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.02.2008 at 12:23AM PDT, ID: 21260744

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.02.2008 at 11:23AM PDT, ID: 21265878

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.04.2008 at 01:15AM PDT, ID: 21279743

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: MS SQL Server, SQL Query Syntax, Microsoft ADP
Sign Up Now!
Solution Provided By: orcic
Participating Experts: 2
Solution Grade: A
 
 
[+][-]04.04.2008 at 01:18AM PDT, ID: 21279753

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628