Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VBA SQL query syntax

Posted on 2014-04-03
5
Medium Priority
?
634 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 448 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 31

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 444 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 39

Accepted Solution

by:
PatHartman earned 448 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

719 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