Easy SQL Question

Posted on 2001-06-22
Last Modified: 2010-05-02
Ok - I can't remember or find what I am doing wrong in this code, Here is the code:
Dim strSQL As String
Dim SrchWord As String
SrchWord = Combo1.Text
If txtCustSearch.Text = txtLastSKU Then Exit Sub
txtLastSKU = txtCustSearch.Text
strSQL = "SELECT FirstName, LastName,CustID,Address,City,State,Zip,Phone1,StateID from Customers " & _
         "Where'" & SrchWord & "Like'" &_ txtCustSearch.Text & "%'"

The problem is in how I am bracketing SrchWord, I can't seem to get it to work, even though I know it's probably easy - I just can't seem to get it.

The combo box is filled with valid field names for search criteria (LastName, FirstName etc:)

Your help is appreciated.


Question by:flosoft
  • 2
LVL 32

Expert Comment

ID: 6219353
Your WHERE statement should be like:

        "Where [" & SrchWord & "] Like '" &_ txtCustSearch.Text & "%'"

This assumes SrchWord is a field name.

Author Comment

ID: 6219407
srchWord is a valid field name selected from a combobox populated with valid field names (LastName, FirstName etc)

I use
Dim SrchWord as String then
SrchWord = Combo1.text

To make srchword = the text of combo1, which at form load is filled with field names listed above and spelled correctly.


Accepted Solution

TheAnswerMan earned 100 total points
ID: 6219412
No.. the problem is that there is a single quote right after the WHERE..  take it out.
and put a space before and after Like

"Where'" & SrchWord & "Like'" &_ txtCustSearch.Text & "%'"
"Where " & SrchWord & " Like '" &_ txtCustSearch.Text & "%'"

Author Comment

ID: 6219437
Hey Thanks Bunches - This is the kinda thing that drives me CRAZY!

It is really appreciated - Excellent Answer.

Thanks to both of you!

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…

828 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