Solved

Convert search string to SQL query

Posted on 2008-06-21
5
154 Views
Last Modified: 2010-04-30
Hi Experts

I'd like to create a query out of a comma delimited "search string"
egSearchstring =  "Item1, Item2, Item3........."
to
sql = sql & "where [Data] like '*Item1*' or [Data] like '*Item2*' or [Data] like '*Item3*' "

I'm thinking of converting "searchstring"  into a "sql string" and then writing
sql = sql & sqlstring

Your ideas on creating that "sql string" would be much appreciated. ( Working in VB6)

Many thanks

Peter

0
Comment
Question by:peterdarazs
  • 3
  • 2
5 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21837042
here we go:
egSearchstring =  "Item1,Item2,Item3........." 
sql = sql & "where [Data] like '*" & join( split(egsearchString, ","), *' or [Data] like '*" ) & "*' "

Open in new window

0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 21837043
a small correction
egSearchstring =  "Item1,Item2,Item3........." 
sql = sql & "where [Data] like '*" & join( split(egsearchString, ","), "*' or [Data] like '*" ) & "*' "

Open in new window

0
 

Author Closing Comment

by:peterdarazs
ID: 31469397
Thanks Angellll

This looks like the solution. I'm a little confused as to the second 'or [Data]' in the routine , but assume this will join to each split item to anothe 'or [Data]' as if it were hard coded. If this is the case , then 100% and complete solution . many thanks

Please let me know if my assumption was incorrect though....

Thanks again

Peter
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21837180
<Grading Comments>
Thanks Angellll

This looks like the solution. I'm a little confused as to the second 'or [Data]' in the routine , but assume this will join to each split item to anothe 'or [Data]' as if it were hard coded. If this is the case , then 100% and complete solution . many thanks

Please let me know if my assumption was incorrect though....

Thanks again

Peter
</Grading Comments>

the first
sql = sql & "where [Data] like '*"
is for the first search item

the join is for what comes between the search items:
join(xxxx , "*' or [Data] like '*" )

and finally:
& "*' "

is to close the last item (which would be the first item if there was only one item)
0
 

Author Comment

by:peterdarazs
ID: 21837284
Ok , that's perfect then, and apprecaite the explanation.

Enjoy your day.
Peter

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Query Syntax Join 4 39
Send outlook email from VBS Script 2 28
Sql Stored Procedure field variable 17 30
ServiceCenter IR Query Expressions 1 16
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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…

776 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