I try to redefine a SQL query with VBA to include the content of a multivalued table field as a where clause but I can't get it to work.
First I gather all the values from the specific multivalued field (formatted as text, can't use numbers due limitations during the import)
BLPN_Query = DLookup("Kostenstellen_Report", "Optionen_Reportgenerierung", "ID=1")
MsgBox will return this (mind the space between ";" and the next text): 34; 44
This string can contain several different text entries. I would like to use the string "BLPN_Query" as a where clause condition. What I've got so far is this:
"WHERE (PROKALK.NK_STK>0) AND (PROKALK.TERMIN>{d '" & Startjahr & "-01-01'}) AND (PROKALK.BLPNR='" & BLPN_Query & "')"
If there is only one entry it works, but in case there are more than one it won't work (obviously). Not sure but I guess the space between the semicolon and the next text is an issue as well as I have to use "IN" instead of "=" but I don't know how to do this.
Solution (thanks to Andre!)
1.) Get data from table (looks like: 34; 35; 36), remove the spaces and replace the semicolon by a comma including single quotes between the elements for the IN clause. Now it looks like: 34','35','36
BLPN_Query = DLookup("Kostenstellen_Report", "Optionen_Reportgenerierung", "ID=1")
BLPN_Query = Replace(BLPN_Query, " ", "")
BLPN_Query = Replace(BLPN_Query, ";", "','")
2.) Include the string within the where clause (and add a single quote before and after the string) --> Final string: '34','35','36'
AND (PROKALK.BLPNR IN ('" & BLPN_Query & "'))"
it's not pretty but will help us until we finally get the new ERP system and can replace all the old stuff