Solved

syntax in dynamic sql access query in vba

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
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…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

777 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