Skip to content
Advertisement

Can not get sql query to run VBA and keep getting errors 3131 error in from clause and 3067 Query must contain at least one table or query

Trying to run a complicated left join on multiple columns and have results show up but won’t run in VBA. I am a noob on this.

I have Tried adding brackets and removed iif statement

SELECT DISTINCT 'Pages ID ' & [p.id] & ', Model ' & '''' & [p.equipmentmodel] & '''' & ', S/Prefix ' & '''' & [p.equipmentserialprefix] & '''' & ', FuelTy ' & '''' & [p.equipmentfueltype] & '''' & ', EngMake ' & '''' & [p.EngineMake] & '''' & ', Engine ' & '''' & [p.enginemodel] & '''' & ', TraMake ' & '''' & [p.transmissionmake] & '''' & ', TraSeries ' & '''' & [p.transmissionseries] & '''' & ', TraModel ' & '''' & [p.transmissionModel] & '''' & ', TranType ' & '''' & [p.transmissiontype] & '''' & ', MastMake ' & '''' & [p.mastmake] & '''' & ', Mast ' & '''' & [p.mastmodel] & '''' & ', CtrlPnl ' & '''' & [p.controlpanel] & '''' & ', Volt ' & '''' & [p.voltage] & '''' & ', CtryCode ' & '''' & [p.countrycode] & '''' & ', Capacity ' & '''' & [p.capacity] & '''' & '; not on Model tbl.' AS Expr1") & _
("

FROM pgs_v AS p LEFT JOIN model AS M ON ([p.CAPACITY] = [M.CAPACITY]) AND ([p.VOLTAGE] = [M.VOLTAGE]) AND ([p.MASTMODEL] = [M.MASTMODEL]) AND ([p.TRANSMISSIONTYPE] = [M.TRANSMISSIONTYPE]) AND ([p.TRANSMISSIONSERIES] = [M.TRANSMISSIONSERIES]) AND ([p.EQUIPMENTSERIALPREFIX] = [M.EQUIPMENTSERIALPREFIX]) AND ([p.EQUIPMENTMODEL] = [M.EQUIPMENTMODEL]) AND ([p.EQUIPMENTFUELTYPE] = [M.EQUIPMENTFUELTYPE]) AND ([p.ENGINEMODEL] = [M.ENGINEMODEL]) AND ([p.ENGINEMAKE] = [M.ENGINEMAKE]) AND ([p.TRANSMISSIONMAKE] = [M.TRANSMISSIONMAKE]) AND ([p.TRANSMISSIONMODEL] = [M.TRANSMISSIONMODEL]) AND ([p.MASTMAKE] = [M.MASTMAKE]) AND ([p.CONTROLPANEL] = [M.CONTROLPANEL]) AND ([p.COUNTRYCODE] = [M.COUNTRYCODE])") & _
("

WHERE ((([p.ENGINEMAKE])<>'' And ([p.ENGINEMAKE]) Is Not Null) AND (([p.equipmentmodel])<>'' And ([p.equipmentmodel]) Is Not Null) AND (([p.equipmentserialprefix])<>'' And ([p.equipmentserialprefix]) Is Not Null) AND (([M.EQUIPMENTMODEL]) Is Null) AND (([p.EQUIPMENTFUELTYPE])<>'' And ([p.EQUIPMENTFUELTYPE]) Is Not Null) AND (([p.ENGINEMODEL])<>'' And ([p.ENGINEMODEL]) Is Not Null) AND (([M.EQUIPMENTFUELTYPE]) Is Null)") & _
("

AND (([M.EQUIPMENTSERIALPREFIX]) Is Null) AND (([M.ENGINEMODEL]) Is Null) AND (([M.ENGINEMAKE]) Is Null) AND (([p.TRANSMISSIONMAKE])<>'' And ([p.TRANSMISSIONMAKE]) Is Not Null) AND (([M.TRANSMISSIONMAKE]) Is Null) AND (([p.TRANSMISSIONSERIES])<>'' And ([p.TRANSMISSIONSERIES]) Is Not Null) AND (([M.TRANSMISSIONSERIES]) Is Null) AND (([p.TRANSMISSIONMODEL])<>'' And ([p.TRANSMISSIONMODEL]) Is Not Null) AND (([M.TRANSMISSIONMODEL]) Is Null) AND (([p.TRANSMISSIONTYPE])<>'' And ([p.TRANSMISSIONTYPE]) Is Not Null) AND (([M.TRANSMISSIONTYPE]) Is Null) AND (([p.MASTMAKE])<>'' And ([p.MASTMAKE]) Is Not Null) AND (([M.MASTMAKE]) Is Null) AND (([p.MASTMODEL])<>'' And") & _
 ("

 ([p.MASTMODEL]) Is Not Null) AND (([M.MASTMODEL]) Is Null) AND (([p.CONTROLPANEL])<>'' And ([p.CONTROLPANEL]) Is Not Null) AND (([M.CONTROLPANEL]) Is Null) AND (([p.VOLTAGE])<>'' And ([p.VOLTAGE]) Is Not Null) AND (([M.VOLTAGE]) Is Null) AND (([p.COUNTRYCODE])<>'' And ([p.COUNTRYCODE]) Is Not Null) AND (([M.COUNTRYCODE]) Is Null) AND (([p.CAPACITY])<>'' And ([p.CAPACITY]) Is Not Null) AND (([M.CAPACITY]) Is Null))")

Advertisement

Answer

the use of of parentheses (“a”)&(“b”) isn’t necessary, you could use a single “a” & “b” to concatenate strings and “a” & _ “b” to join lines of code, try this to construct your query

Sub JoinQueryString()
Dim SqlQuery As String
SqlQuery = "SELECT DISTINCT 'Pages ID ' & [p.id] & ', Model ' & '''' & [p.equipmentmodel] & '''' & ', S/Prefix ' & '''' & [p.equipmentserialprefix] & '''' & ', FuelTy ' & '''' & [p.equipmentfueltype] & '''' & ', EngMake ' & '''' & [p.EngineMake] & '''' & ', Engine ' & '''' & [p.enginemodel] & '''' & ', TraMake ' & '''' & [p.transmissionmake] & '''' & ', TraSeries ' & '''' & [p.transmissionseries] & '''' & ', TraModel ' & '''' & [p.transmissionModel] & '''' & ', TranType ' & '''' & [p.transmissiontype] & '''' & ', MastMake ' & '''' & [p.mastmake] & '''' & ', Mast ' & '''' & [p.mastmodel] & '''' & ', CtrlPnl ' & '''' & [p.controlpanel] & '''' & ', Volt ' & '''' & [p.voltage] & '''' & ', CtryCode ' & '''' & [p.countrycode] & '''' & ', Capacity ' & '''' & [p.capacity] & '''' & '; not on Model tbl.' AS Expr1" & _
" FROM pgs_v AS p LEFT JOIN model AS M ON ([p.CAPACITY] = [M.CAPACITY]) AND ([p.VOLTAGE] = [M.VOLTAGE]) AND ([p.MASTMODEL] = [M.MASTMODEL]) AND ([p.TRANSMISSIONTYPE] = [M.TRANSMISSIONTYPE]) AND ([p.TRANSMISSIONSERIES] = [M.TRANSMISSIONSERIES]) AND ([p.EQUIPMENTSERIALPREFIX] = [M.EQUIPMENTSERIALPREFIX]) AND ([p.EQUIPMENTMODEL] = [M.EQUIPMENTMODEL]) AND ([p.EQUIPMENTFUELTYPE] = [M.EQUIPMENTFUELTYPE]) AND ([p.ENGINEMODEL] = [M.ENGINEMODEL]) AND ([p.ENGINEMAKE] = [M.ENGINEMAKE]) AND ([p.TRANSMISSIONMAKE] = [M.TRANSMISSIONMAKE]) AND ([p.TRANSMISSIONMODEL] = [M.TRANSMISSIONMODEL]) AND ([p.MASTMAKE] = [M.MASTMAKE]) AND ([p.CONTROLPANEL] = [M.CONTROLPANEL]) AND ([p.COUNTRYCODE] = [M.COUNTRYCODE]) " & _
" WHERE ((([p.ENGINEMAKE])<>'' And ([p.ENGINEMAKE]) Is Not Null) AND (([p.equipmentmodel])<>'' And ([p.equipmentmodel]) Is Not Null) AND (([p.equipmentserialprefix])<>'' And ([p.equipmentserialprefix]) Is Not Null) AND (([M.EQUIPMENTMODEL]) Is Null) AND (([p.EQUIPMENTFUELTYPE])<>'' And ([p.EQUIPMENTFUELTYPE]) Is Not Null) AND (([p.ENGINEMODEL])<>'' And ([p.ENGINEMODEL]) Is Not Null) AND (([M.EQUIPMENTFUELTYPE]) Is Null) " & _
" AND (([M.EQUIPMENTSERIALPREFIX]) Is Null) AND (([M.ENGINEMODEL]) Is Null) AND (([M.ENGINEMAKE]) Is Null) AND (([p.TRANSMISSIONMAKE])<>'' And ([p.TRANSMISSIONMAKE]) Is Not Null) AND (([M.TRANSMISSIONMAKE]) Is Null) AND (([p.TRANSMISSIONSERIES])<>'' And ([p.TRANSMISSIONSERIES]) Is Not Null) AND (([M.TRANSMISSIONSERIES]) Is Null) AND (([p.TRANSMISSIONMODEL])<>'' And ([p.TRANSMISSIONMODEL]) Is Not Null) AND (([M.TRANSMISSIONMODEL]) Is Null) AND (([p.TRANSMISSIONTYPE])<>'' And ([p.TRANSMISSIONTYPE]) Is Not Null) AND (([M.TRANSMISSIONTYPE]) Is Null) AND (([p.MASTMAKE])<>'' And ([p.MASTMAKE]) Is Not Null) AND (([M.MASTMAKE]) Is Null) AND (([p.MASTMODEL])<>'' AND " & _
" ([p.MASTMODEL]) Is Not Null) AND (([M.MASTMODEL]) Is Null) AND (([p.CONTROLPANEL])<>'' And ([p.CONTROLPANEL]) Is Not Null) AND (([M.CONTROLPANEL]) Is Null) AND (([p.VOLTAGE])<>'' And ([p.VOLTAGE]) Is Not Null) AND (([M.VOLTAGE]) Is Null) AND (([p.COUNTRYCODE])<>'' And ([p.COUNTRYCODE]) Is Not Null) AND (([M.COUNTRYCODE]) Is Null) AND (([p.CAPACITY])<>'' And ([p.CAPACITY]) Is Not Null) AND (([M.CAPACITY]) Is Null))"
Debug.Print SqlQuery
End Sub
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement