简体   繁体   中英

IIF statement MS ACCESS VBA SQL

I have 3 tables

ConnectivityFile where I have the ids for some properties

CHAINDD where I have the hotel codes (they can be repeated) for the same properties

Export where I want to have all the hotel codes and their ids (a join for the previous tables)

I can achieve that with the query below

query2 = "SELECT ConnectivityFile.ID as [Hotel ID], ConnectivityFile.HotelCode  
FROM ConnectivityFile 
INNER JOIN (SELECT DISTINCT CHAINDD.HOTELCODE as [Hotel Code] from CHAINDD) as T 
ON ConnectivityFile.HotelCode = T.[Hotel Code]"

DoCmd.RunSQL "INSERT INTO Export " & query2

Now the issue I am facing is that let's say the hotel code from table CHAINDD is not present on table ConnectivityFile. I would like to point that out as "Not Connected" as the ID on the export table.

How can achieve that? I know there is the IIF statement when I have done some trial and error with no success. Where do you place the IIf statement?

Thank you very much for all your help and time

as long as your query actually works, the IIF goes something like this:
query2 = "SELECT IIF(ISNULL(T.HOTELCODE), ""Not Connected"", ConnectivityFile.ID) as [Hotel ID], ConnectivityFile.HotelCode
FROM ConnectivityFile LEFT JOIN (SELECT DISTINCT CHAINDD.HOTELCODE as [Hotel Code] from CHAINDD) as T ON ConnectivityFile.HotelCode = T.[Hotel Code]"

basically, if the code is there, use it, otherwise put a string staying "Not connected"
Note that the hotel code field in Export must be able to handle NON-unique data - ie: not be a primary key.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM