[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now



Posted on 2004-10-31
Medium Priority
Last Modified: 2008-02-26
I hope this is an easy question for someone.  I am using ADO to access some data.  I need to write a SQL statement to narrow that data down.  I can use the LIKE statement successfully for one option, but not multiple.  Here is some code.

'This string will work
strSQL = "Field1 LIKE '%" & txtOption1 & "%'"

'I need something like this to work
strSQL = "Field1 LIKE '%" & txtOption1 & "&' AND Field2 LIKE '%" & txtOption2 & "%' AND Field3 LIKE '%" & txtOption3 & "%'"

Set rsRecordSet = New ADODB.Recordset
With rsRecordSet
    .CursorType = adOpenStatic
    .CursorLocation = adUseClient
    .LockType = adLockPessimistic
    .Source = "SELECT * FROM Table1 WHERE " & strSQL
    .ActiveConnection = connConnection
End With

I get the follwoing error when I run it with the SQL statement that has multiple LIKE statements in it.  ( I put "test" in txtOption2)

**  Syntex error (missing operator) in query expression 'AND Field2 LIKE '%test%"  **

Thanks for the help!
Question by:APlusComp247
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
  • 2
  • 2
  • +2
LVL 53

Expert Comment

by:Ryan Chong
ID: 12457577

strSQL = "Field1 LIKE '%" & replace(txtOption1,"'","''") & "' AND Field2 LIKE '%" & replace(txtOption2,"'","''") & "%' AND Field3 LIKE '%" & replace(txtOption3,"'","''") & "%'"


strSQL = "Field1 LIKE '%" & replace(txtOption1,"'","''") & "%' AND Field2 LIKE '%" & replace(txtOption2,"'","''") & "%' AND Field3 LIKE '%" & replace(txtOption3,"'","''") & "%'"

LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 12457875
Try this
strSQL = "Field1 LIKE '%" & Replace(txtOption1,"'","''") & "%' AND Field2 LIKE '%" & Replace(txtOption2,"'","''") & "%' AND Field3 LIKE '%" & Replace(txtOption3,"'","''")  & "%'"

u put an extra   &   instead of   %


LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 12457891

sorry i am late
ryancys  deserves it

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 12457915
I understand what you are getting at, but I haven't used any apostraphes in the textboxes so the Replace statement shouldn't change anything.... right?

I tried that and it didn't work.

Any more ideas??
LVL 18

Accepted Solution

JR2003 earned 2000 total points
ID: 12458184
What is in the value of strSQL when you execute the command?  Can you paste it in here so we can have a look?
Just add the line :
Debug.Print "SELECT * FROM Table1 WHERE " & strSQL
when you set the '.Source' to make it appear in the immediate window.

Which database are you using?
LVL 18

Expert Comment

ID: 12459051
There is a very simple way to do this using the "Microsoft Script Control 1.0". All you need to do is create a string which is the expression you want to evaluate and just let the "Microsoft Script Control 1.0" evaluate it.

You can set the 'strExpression' string to any vb expression e.g.: "3 / 4" or "5 * (6 - 1)" etc.

Private Sub Command1_Click()
    Dim dblResult As Double
    Dim strExpression As String
    Dim MyScripControl As Object
    Set MyScripControl = CreateObject("ScriptControl")

    strExpression = "3 / 4"  '<---  You can put any expression you like here

    MyScripControl.Language = "VBScript"
    dblResult = MyScripControl.Eval(strExpression )
    MsgBox dblResult

End Sub
LVL 18

Expert Comment

ID: 12459055
Please ignore the last comment, wrong question answered!
LVL 28

Expert Comment

ID: 12459597
Try to put fields name into a brackets :
[Field1] Like ...

Author Comment

ID: 12460338
Ok, I made a stupid mistake.  I was using a few IF  statements to create the SQL statement depending on whether data was entered into the textbox or not.  I used the following

strSQL = " AND Field1 LIKE '%" & txtOption1 & "%'"

I quickly figured out after using the debug.print than I needed the following

strSQL = strSQL & " AND Field1 LIKE '%" & txtOption1 & "%'"

I was so unsure of my SQL syntax that I completely forgot a little simple debuging.

Thanks for all the help!!

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

649 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