Access sql runs as a query but not from docmd.sql

I have a query which runs from the query grid fine. I need to make the sql dynamic but i cannot get it to run from vb it reports back error as attached and not sure why.

    Case 4
        ' all events
        DoCmd.RunSQL "SELECT v.CapCode, Min(v.ChangeYearMonth) AS minYM, Max(v.ChangeYearMonth) AS maxYM, First(v.BHPPrev) AS Tprev," _
        & " Last(v.BHPChange) AS Tchange, Round(Abs([BHPPrev]-[BHPChange])) AS DataVariance, CapCurrentCodes.CAPid_CAPcat, [CapToMvris-CW].CapCode1_1, [CapToMvris-CW].MvrisCode, SMMT.[MVRIS CODE], SMMT.[CALC BHP], Round(Abs([BHPchange]-[calc bhp])) AS [Variance to Match], [CapToMvris-CW].CapCategory_1" _
        & " FROM (((SELECT DISTINCT [CapCode], ChangeYearMonth, BHPPrev, BHPChange FROM TblBHP)  AS v LEFT JOIN CapCurrentCodes ON v.CapCode = CapCurrentCodes.CAPid_CAPcat) LEFT JOIN [CapToMvris-CW] ON CapCurrentCodes.CapVehicleID = [CapToMvris-CW].CapCode1_1) LEFT JOIN SMMT ON [CapToMvris-CW].MvrisCode = SMMT.[MVRIS CODE]" _
        & " GROUP BY v.CapCode, Round(Abs([BHPPrev]-[BHPChange])), CapCurrentCodes.CAPid_CAPcat, [CapToMvris-CW].CapCode1_1, [CapToMvris-CW].MvrisCode, SMMT.[MVRIS CODE], SMMT.[CALC BHP], Round(Abs([BHPchange]-[calc bhp])), [CapToMvris-CW].CapCategory_1" _
        & " HAVING (((First(v.BHPPrev)) Is Not Null Or (First(v.BHPPrev)) Like ""0"") And ((Last(v.BHPChange)) Is Not Null Or (Last(v.BHPChange)) Like ""0"") And ((CapCurrentCodes.CAPid_CAPcat) Is Not Null) And (([CapToMvris-CW].CapCategory_1)=IIf(Right([capcode],1)=""C"",""Car"",IIf(Right([capcode],1)=""L"",""LCV"",""Motorcycle"")) Or ([CapToMvris-CW].CapCategory_1) Is Null) And ((Nz(First(v.BHPPrev),""""))<>Last(v.BHPChange)));"

Open in new window

eeexample.PNG
PeterBaileyUkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

John TsioumprisSoftware & Systems EngineerCommented:
better to create a temp string variable(strSQL) ...set it to your SQL and before Docmd.RunSQL (strSQL)
in debug view transfer it to immediate window to see what is actually passed to run
 
? strSQL

Open in new window

Dale FyeOwner, Dev-Soln LLCCommented:
First, runSQL is designed to be used with an Action query (append, update, delete), not a select query.

I agree with John.

Dim strSQL as string
strSQL = "..."
debug.print strSQL

Then copy the SQL from the immediate window and paste it into the SQL view of the query designer.  This will generally highlight where you error is.  Then you can go back, edit the SQL string and rerun until it works.

BTW, I would encourage you to use the Execute method rather than runSQL.  This accomplishes two things.
1.  When you use docmd.runSQL, you will usually get warning messages (you are about to ...) which are annoying.  The only way to remove these is by docmd.SetWarnings False, but this is dangerous because if you forget to turn them back on, you could do something you don't want.
2.  With the:

currentdb.Execute strSQL, dbFailOnError

syntax, you don't get the warning messages, but you can add error handling to your application to handle errors caused by the Execute method.
Gustav BrockCIOCommented:
DoCmd.RunSQL  is for action queries only.

You need a recordset, then pass your SQL string:

    Set rst = CurrentDb.OpenRecordset(strSQL)

/gustav

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.