Help with Update Query using Case Statement (1 Viewer)

netbytes

Registered User.
Local time
Yesterday, 23:52
Joined
Aug 11, 2008
Messages
33
Hi!

I just want to know how can I convert this into MS ACCESS CODE using UPDATE query?

If QA > 10 then Avail = "Instock"
ElseIf QA < = 10 then Avail = "Lowstock"
ElseIf QA = 0 then Avail = "Outofstock"
ElseIf QA = 'discontinued' then Avail = "discontinued"
End If

How to use case here?
QA field is in the first table
Avail field is in the second table
so i used inner join.

May I know if im going to use a Module?
This is just an example.

Thank you. Hope someone can answer my questions. :D
 
Welcome aboard. There is no need to store the calculated value and in fact it would be wrong to do so. If you store the result of the case statement, it would only be valid for the brief moment in time when you stored it. As soon as someone updated the database, the calculated value could be wrong. That is why it is incorrect to store values such as this.

Access SQL doesn't include a Case statement but it does include the IIf() function which can be used to return the value you want.

Select IIf(QA > 10, "Instock", IIf(QA < = 10, "Lowstock", IIf(QA = 0, "Outofstock", "Discontinued"))) As Avail, fld2, fld3, fld4, ....
From YourTable
 
Thank you!

Welcome aboard. There is no need to store the calculated value and in fact it would be wrong to do so. If you store the result of the case statement, it would only be valid for the brief moment in time when you stored it. As soon as someone updated the database, the calculated value could be wrong. That is why it is incorrect to store values such as this.

Access SQL doesn't include a Case statement but it does include the IIf() function which can be used to return the value you want.

Select IIf(QA > 10, "Instock", IIf(QA < = 10, "Lowstock", IIf(QA = 0, "Outofstock", "Discontinued"))) As Avail, fld2, fld3, fld4, ....
From YourTable


oh! i see. but my problem is this.

I need 1 Update query that will update my two tables.
The tables are QualityMatters and Teledynamics

My project is how to combine these queries into 1 UPDATE Query:

Queries Named as follows:
1. instock query
2. outofstock query
3. lowstock query
4. discontinued query

All running to update my tables but needs to click 1 by 1.
So I need those in one query. I dont know what im going to use. I used a lot of statements but still Im really confused.

Example: I named this query as instock query

UPDATE Teledynamics INNER JOIN QualityMatters ON Teledynamics.[Item Number] = QualityMatters.vendor_partno SET QualityMatters.customfield1 = "IN STOCK"
WHERE (((Teledynamics.[Quantity Available])>10));

Another, lowstock query
UPDATE Teledynamics INNER JOIN QualityMatters ON Teledynamics.[Item Number] = QualityMatters.vendor_partno SET QualityMatters.customfield1 = "LOW STOCK"
WHERE (((Teledynamics.[Quantity Available])>0 And (Teledynamics.[Quantity Available])<10));

another one, outofstock query
UPDATE Teledynamics INNER JOIN QualityMatters ON Teledynamics.[Item Number] = QualityMatters.vendor_partno SET QualityMatters.customfield1 = "OUT OF STOCK"
WHERE (((Teledynamics.[Quantity Available])=0));

and the discontinued query
UPDATE Teledynamics INNER JOIN QualityMatters ON Teledynamics.[Item Number] = QualityMatters.vendor_partno SET QualityMatters.customfield1 = "Discontinued"
WHERE (((Teledynamics.[Quantity Available]) Like 'DISCONTINUED'));

oh! well, i need to use just 1 update query to run and update my two tables. I have 4 queries so I want it to be in only one query which is using UPDATE and SELECT Statements.

Is there a way to combine all of this using 1 UPDATE QUERY ONLY?
Hope you can answer. THANK YOU!
 
Remove the where clause and use the IIf() in place of the "..." part of the set. You'll have to change QA to [Quantity Available] in my example.

I repeat my statement that you do NOT need to store the text string and it is WRONG to so so. Whenever you need it, join the two tables and use the IIf() to return the text value.
 
Thanks Alot

Thank you sir!

Hahaha i got it already..
Now my update query is running and in only one query unlike before..

thanks!! more power
 

Users who are viewing this thread

Back
Top Bottom