Link to home
Start Free TrialLog in
Avatar of PeterBaileyUk
PeterBaileyUk

asked on

syntax in dynamic sql access query in vba

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
Avatar of James Elliott
James Elliott
Flag of United Kingdom of Great Britain and Northern Ireland image

Can you post the whole routine please.
Avatar of PeterBaileyUk
PeterBaileyUk

ASKER

 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
ASKER CERTIFIED SOLUTION
Avatar of PatHartman
PatHartman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

I see finally what you meant code is now corrected and working.
thank you