Solved

syntax in dynamic sql access query in vba

Posted on 2014-10-06
5
232 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 35

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

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…
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…
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…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

839 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