Solved

SQL (Variable) using LIKE

Posted on 2002-03-26
6
149 Views
Last Modified: 2010-05-02
Hello,

I want to search all records that start with the letter "A", but with a variable.

Dim strA As String
strA = "A"

'With Doesn't Work.......

sSql = "Select * FROM tbl_Vendors " _
        & "WHERE(tbl_Vendors.fld_Vendor <> NULL) " _
        & "Add tbl_Vendors.fld_Vendor Like ' strA ' " _
        & "ORDER BY fld_Vendor ASC;"

Thanks, ADawn


0
Comment
Question by:ADawn
[X]
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
6 Comments
 
LVL 18

Accepted Solution

by:
mdougan earned 10 total points
ID: 6897495
Concatenate your string variable with the SQL and make sure you put a percent sign after the string variable, but inside of the single quotes.  Also, you had "ADD tbl_Vendors" instead of "AND tbl_Vendors"

sSql = "Select * FROM tbl_Vendors " _
       & "WHERE(tbl_Vendors.fld_Vendor <> NULL) " _
       & "AND tbl_Vendors.fld_Vendor Like '" & strA & "%'" _
       & "ORDER BY fld_Vendor ASC;"
0
 
LVL 18

Expert Comment

by:mdougan
ID: 6897513
Also, be careful of extra spaces.  Here was your code:

Like ' strA '  

You have a space between the first single quote and the strA, and then a space after - before the next single quote.  The database would be forced to try to find records that include those spaces in those positions.  So, you'd want to make sure your code looked more like:

Like 'strA'

But, again, this is going to look for an exact match on the search string, so, follow it with a percent sign if you want to match strings that "start with" your search criteria:


Like 'strA%'

I know that strA was the name of your variable, but I'm just using it here as an example search criteria.
0
 
LVL 14

Expert Comment

by:puranik_p
ID: 6898351
If you are using ACCESS, make it
Like 'strA*'
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 18

Expert Comment

by:mdougan
ID: 6898359
Later versions of Access use the percent sign (%) instead of the asterisk (*)
0
 

Expert Comment

by:dboero
ID: 6901632
I use this code with Access Databases:

" WHERE Descripcion Like '" & Text1.Text & "%' order by Descripcion"

0
 

Author Comment

by:ADawn
ID: 6902464
Great Thanks, ADawn
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month10 days, 5 hours left to enroll

623 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