Solved

syntax in dynamic sql access query in vba

Posted on 2014-10-06
5
226 Views
Last Modified: 2014-10-06
I am building up the having clause with:

 Do Until .EOF
                                    
                                  If start = True Then
                                    FilterWord = " like (*" & .Fields("FilterWord").Value & "*)"
                                    start = False
                                  Else
                                        FilterWord = FilterWord & " AND like (*" & .Fields("FilterWord").Value & "*)"
                                        
                                  End If
                                          
                                .MoveNext
                              Loop

Open in new window


which produces this:
HAVING (((TblReverseFilter.StrOriginal) Like ("*bus*") And (TblReverseFilter.StrOriginal) Like ("*med*") And (TblReverseFilter.StrOriginal) Like ("*prof*")));

it wont run the query, how do I create the syntax like this:

HAVING (((TblReverseFilter.StrOriginal)  like (*bus*) AND like (*med*) AND like (*prof*)));

which was taken from a handmade query in the grid that does work
0
Comment
Question by:PeterBaileyUk
  • 3
5 Comments
 
LVL 12

Expert Comment

by:James Elliott
ID: 40363945
Can you post the whole routine please.
0
 

Author Comment

by:PeterBaileyUk
ID: 40363967
 Dim SelectFinal As String
                  Dim FromFinal As String
                  Dim GroupFinal As String
                  Dim HavingFinal As String
                     Dim QryFinal As String
                    
      
                    SelectFinal = "INSERT INTO TblReverseFiltTemp ( StrOriginal ) SELECT TblReverseFilter.StrOriginal"
                    FromFinal = " FROM TblReverseFilter"
                    GroupFinal = " GROUP BY TblReverseFilter.StrOriginal, TblReverseFilter.LngScore"
                    HavingFinal = " HAVING (((TblReverseFilter.StrOriginal) " & FilterWord & " ));"
                    
                    
          


                    

                        QryFinal = SelectFinal & FromFinal & GroupFinal & HavingFinal
                        Debug.Print "QryFinal: " & QryFinal
                    DoCmd.RunSQL QryFinal

Open in new window


as requested

I didnt post as its the having causing the problem
0
 
LVL 34

Accepted Solution

by:
PatHartman earned 500 total points
ID: 40363969
HAVING (((TblReverseFilter.StrOriginal)  like (*bus*) AND like (*med*) AND like (*prof*)));
Is invalid syntax.  In a compound condition, you MUST repeat the A operand.  Then there's the problem of the relational operator.  AND requires ALL conditions to be true and OR requires ANY to be true.  Given that you are using the same A operand, it CANNOT be three values at once so the condition will always return false.  And the final error (that I see) is, the B operand should not be enclosed in parentheses although that might not cause a syntax error.

HAVING TblReverseFilter.StrOriginal   like *bus* OR TblReverseFilter.StrOriginal like *med* OR TblReverseFilter.StrOriginal like *prof*


Also, it is more efficient to use WHERE than HAVING to reduce the number of rows that need to be aggregated.  WHERE is applied before aggregation and HAVING is applied after aggregation.  Therefore HAVING is used against columns that have been aggregated such as when you are looking for a sum or avg > some value.
0
 

Author Comment

by:PeterBaileyUk
ID: 40364009
The syntax was created by access I merely took the sql from the query and wanted to make it dynamic i selected group by as the query would otherwise have created duplicates.


SELECT TblReverseFilter.StrOriginal
FROM TblReverseFilter
GROUP BY TblReverseFilter.StrOriginal, TblReverseFilter.LngScore
HAVING (((TblReverseFilter.StrOriginal) Like ("*bus*") And (TblReverseFilter.StrOriginal) Like ("*med*") And (TblReverseFilter.StrOriginal) Like ("*prof*")));

Open in new window

0
 

Author Closing Comment

by:PeterBaileyUk
ID: 40364078
I see finally what you meant code is now corrected and working.
thank you
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

746 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

12 Experts available now in Live!

Get 1:1 Help Now