Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Alternate key if no results 2

Status
Not open for further replies.

jv6886

Programmer
Dec 31, 2002
46
US
Say I have a table that looks something like the following:
Code:
State     Value
-----     -----
 FL         1
 FL         5
 FL         9
 AR         2
 AR         5
 MI         7
 MI         8
 XX         1
 XX         5
I use an SQL query to retrieve the records for a given state. If the state requested turns up no records, I want to use the alternate results from 'XX'.

Does anyone know of any way to do this using a single SQL statement?

Thank you!

 
No I don't think there is. The reason being that the result set has to be evaluated (in this case to get the RecordCount) and that isn't the responsibility of a Select statement. If you need help with doing it with more than one Select statement let us know.

Paul
 
You might need either a subquery or DCount(). The subquery solution is
SELECT tblStateVals.*
FROM tblStateVals
WHERE tblStateVals.State=IIf((Select Count(*) FROM tblStateVals WHERE State = [Enter State])=0,"XX",[Enter State]);

Duane
MS Access MVP
 
Works perfectly dhookom. Thanks a million!

wcprog
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top