Solved

VBA SQL query syntax

Posted on 2014-04-03
5
628 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 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 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HasData 9 37
MS Access to SQL Conversion - Bit and Yes/No Datatypes 12 46
VBA code won't run Delete Query 5 25
Access coding 2 13
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
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…

895 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

17 Experts available now in Live!

Get 1:1 Help Now