Solved

syntax in dynamic sql access query in vba

Posted on 2014-10-06
5
234 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
[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
  • 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 37

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
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, when working with VBA, learn some techniques for writing readable and easily maintained code.

751 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