We help IT Professionals succeed at work.
Get Started

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

66 Views
Last Modified: 2016-02-01
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
Comment
Watch Question
CERTIFIED EXPERT
Most Valuable Expert 2015
Distinguished Expert 2018
Commented:
This problem has been solved!
Unlock 3 Answers and 3 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE