 |
| Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

July 11th, 2007, 06:53 AM
|
|
Friend of Wrox
|
|
Join Date: Apr 2007
Posts: 110
Thanks: 1
Thanked 2 Times in 2 Posts
|
|
IFF () in SQL
Hi
I am trying to change IIf([Contacts].[APC]='XXXXX','Deleted','')in this query but don't know to change is SQL as db is transfering to SQL
here's Query
criteria = "SELECT Contacts.Contact, Customers.Customer, Customers.CID, IIf([Contacts].[APC]='XXXXX','Deleted','') AS Status FROM Customers INNER JOIN Contacts ON Customers.CID = Contacts.CID WHERE (Contacts.Contact Like '" & Me!SearchFilter & "*') AND (" & GetCondition("Reps", "rep") & ") ORDER BY Contacts.Contact, Customers.Customer, IIf([Contacts].[APC]='XXXXX','Deleted','');"
Thanks
|
|

July 11th, 2007, 07:21 AM
|
|
Friend of Wrox
|
|
Join Date: Apr 2007
Posts: 110
Thanks: 1
Thanked 2 Times in 2 Posts
|
|
Solution! :)
SELECT Contacts.Contact,
Customers.Customer,
Customers.CID,
CASE WHEN [Contacts].[APC]='XXXXX' THEN 'Deleted' ELSE '' END AS Status
FROM Customers
INNER JOIN Contacts
ON Customers.CID = Contacts.CID
WHERE (Contacts.Contact Like 'q%')
--AND (" & GetCondition("Reps", "rep") & ")
ORDER BY 1,2,4
|
|

July 11th, 2007, 07:27 AM
|
|
Friend of Wrox
|
|
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
|
|
I think that part will work. You want to change Contacts.APC to the string "deleted", otherwise display an empty string?
Also, if you are going to SQL Server, then you should change this to:
criteria = "SELECT Contacts.Contact, Customers.Customer, Customers.CID, IIf([Contacts].[APC]='XXXXX','Deleted','') AS Status FROM Customers INNER JOIN Contacts ON Customers.CID = Contacts.CID WHERE (Contacts.Contact Like '" & Me!SearchFilter & "%') AND (" & GetCondition("Reps", "rep") & ") ORDER BY Contacts.Contact, Customers.Customer, Status"
Note: Change the wildcard * to %, and omit the ";" at the end of the string. Also, in the ORDER BY clause, refer to the alias of the column, not the expression.
I think that will work.
An even better solution would be to build this as a view on the SQL Server since the server will optimize the query, and also reduce data on the wire.
The View would be:
"SELECT Contacts.Contact, Customers.Customer, Customers.CID, IIf([Contacts].[APC]='XXXXX','Deleted','') AS Status
FROM Customers INNER JOIN Contacts ON Customers.CID = Contacts.CID
ORDER BY Contacts.Contact, Customers.Customer, Status"
Then you would do this:
criteria = "WHERE [Contact] Like '" & Me!SearchFilter & "%') AND (" & GetCondition("Reps", "rep") & ")"
Then your SQL string would be:
sSQL = "SELECT * FROM vwMyViewOnSQLServer '" & criteria & "'"
Did that help any?
mmcdonal
|
|

July 11th, 2007, 07:28 AM
|
|
Friend of Wrox
|
|
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
|
|
Oh well, forget it then. =)
mmcdonal
|
|

July 17th, 2007, 04:43 AM
|
|
Friend of Wrox
|
|
Join Date: Apr 2007
Posts: 110
Thanks: 1
Thanked 2 Times in 2 Posts
|
|
Thanks very much for that help
|
|

July 17th, 2007, 05:52 AM
|
|
Friend of Wrox
|
|
Join Date: Apr 2007
Posts: 110
Thanks: 1
Thanked 2 Times in 2 Posts
|
|
Hi mmcdonal
In Addition, I found
IIF() doesn't recognize by SQL server so better to change IIF() in CASE and it help as well
Thanks
|
|
 |