Solved

VBA SQL query syntax

Posted on 2014-04-03
5
633 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 27

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 38

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

717 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