简体   繁体   中英

Union ALL query failed in VBA but ok in SQL

I Tried to run the below Query but its showing an error incorrect syntax near 'unionselect' Can anyone help?

strsql = "select distinct a.LOCID,locnum,locname,streetname,city,county,statecode,state,peril,SITEDEDAMT,SITELIMAMT,COMBINEDDEDAMT,COMBINEDLIMAMT from eqdet a inner join loccvg b on a.LOCID=b.locid inner join loc c on b.LOCID=c.LOCID where PERIL=1" & _
   "union" & _
  "select distinct a.LOCID,locnum,locname,streetname,city,county,statecode,state,peril,SITEDEDAMT,SITELIMAMT,COMBINEDDEDAMT,COMBINEDLIMAMT from hudet a  inner join loccvg b on a.LOCID=b.locid inner join loc c on b.LOCID=c.LOCID where peril=2" & _
  "union" & _
    "select distinct a.LOCID,locnum,locname,streetname,city,county,statecode,state,peril,SITEDEDAMT,SITELIMAMT,COMBINEDDEDAMT,COMBINEDLIMAMT from todet a  inner join loccvg b on a.LOCID=b.locid inner join loc c on b.LOCID=c.LOCID where peril=3" & _
    "union" & _
   "select a.LOCID,locnum,locname,streetname,city,county,statecode,state,peril,SITEDEDAMT,SITELIMAMT,COMBINEDDEDAMT,COMBINEDLIMAMT from fldet a  inner join loccvg b on a.LOCID=b.locid inner join loc c on b.LOCID=c.LOCID where peril=4" & _
   "union" & _
 "select distinct a.LOCID,locnum,locname,streetname,city,county,statecode,state,peril,SITEDEDAMT,SITELIMAMT,COMBINEDDEDAMT,COMBINEDLIMAMT from frdet a  inner join loccvg b on a.LOCID=b.locid inner join loc c on b.LOCID=c.LOCID where peril=5" & _
 "union" & _
 "select distinct a.LOCID,locnum,locname,streetname,city,county,statecode,state,peril,SITEDEDAMT,SITELIMAMT,COMBINEDDEDAMT,COMBINEDLIMAMT from trdet a  inner join loccvg b on a.LOCID=b.locid inner join loc c on b.LOCID=c.LOCID where peril=6"

Looking at the error message I suspect you are concatinating a string to make the query. unionselect should be two words, seperated by a space. ( union select ) I think that in your code spaces are missing to separate keywords.

See:

"...where PERIL=1" & _ "union" & _ "select distinct..."

will produce

...where PERIL=1unionselect distinct...

output. Correct code (notice the spaces around the union):

"...where PERIL=1" & _ " union " & _ "select distinct ..."  

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