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

PeterBaileyUk used Ask the Experts™
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

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
John TsioumprisSoftware & Systems Engineer
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 LLC
Most Valuable Expert 2014
Top Expert 2010
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.
Most Valuable Expert 2015
Distinguished Expert 2018
DoCmd.RunSQL  is for action queries only.

You need a recordset, then pass your SQL string:

    Set rst = CurrentDb.OpenRecordset(strSQL)


Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial