Solved

VBA SQL query syntax

Posted on 2014-04-03
5
627 Views
Last Modified: 2014-04-07
Good Afternoon Experts,

I am having an issue with running a sql query within access 2010 VBA.

What is wrong with it?

strSQL = "SELECT DISTINCT Sum(_ChartDBSource_Template.Jan) AS SumOfJan " & vbCrLf & _
"FROM MOR_ChartDBSource_Template " & vbCrLf & _
"WHERE (((MOR_ChartDBSource_Template.CHARTDB_BIN) Like ""CalcHC"") AND ((_ChartDBSource_Template._GROUP) Like ""*Design B Other*"" Or (_ChartDBSource_Template._GROUP) Like ""*Design C Other*"" Or (_ChartDBSource_Template._GROUP) Like ""*Design CD Other*"" Or (_ChartDBSource_Template._GROUP) Like ""*Design Large Other*"" Or (_ChartDBSource_Template._GROUP) Like ""*Design Other*""));"

Thanks in advance !!
0
Comment
Question by:UserName935
5 Comments
 
LVL 7

Assisted Solution

by:COACHMAN99
COACHMAN99 earned 112 total points
ID: 39976324
not sure what the error is but
try removing the double quotes e.g. LIKE '*Design*'
and why embed linefeeds?

e.g.
strSQL = "SELECT DISTINCT Sum(_ChartDBSource_Template.Jan) AS SumOfJan FROM MOR_ChartDBSource_Template WHERE (((MOR_ChartDBSource_Template.CHARTDB_BIN) Like 'CalcHC') AND ((_ChartDBSource_Template._GROUP) Like '*Design B Other*' Or (_ChartDBSource_Template._GROUP) Like '*Design C Other*' Or (_ChartDBSource_Template._GROUP) Like '*Design CD Other*' Or (_ChartDBSource_Template._GROUP) Like '*Design Large Other*' Or (_ChartDBSource_Template._GROUP) Like '*Design Other*'));"
0
 
LVL 26

Expert Comment

by:MacroShadow
ID: 39976388
Remove  all & vbCrLf & _
0
 
LVL 30

Expert Comment

by:hnasr
ID: 39976599
May you list a record, and the how do you expect it to appear.
0
 
LVL 61

Assisted Solution

by:mbizup
mbizup earned 111 total points
ID: 39976804
The double quotes *should* be fine, since you have them in pairs.

Check your spelling/typing /table names.

You have several instances of _ChartDBSource_Template which probably should be MOR_ChartDBSource_Template

(You seem to have omitted a MOR_ prefix in several places)
0
 
LVL 34

Accepted Solution

by:
PatHartman earned 112 total points
ID: 39976825
There is nothing variable about this query so there is no reason for it to not be a querydef.  Even if for some reason you prefer embedded SQL, creating the query and getting out the syntax errors is much easer with the QBE.

If your BE is Jet/ACE it is more efficient to store the queries as querydefs.  That way Access calculates the execution plan the first time you run the query and saves it.  Then every other time you run the query, Access can save the step of calculating a new execution plan.  With embedded SQL, Access must calculate a new execution plan every time you run the query.  The overhead is minimal but inefficient methods eventually build up to make applications sluggish.  If the BE is SQL Server, the query will be sent to the server which will calculation the execution plan so it makes no difference whether the query is a querydef or embedded string.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now