Print Page | Close Window

Search for MULTIPLE words within ONE field??

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Classic ASP Discussion
Forum Description: Discussion on Active Server Pages (Classic ASP).
URL: https://forums.webwiz.net/forum_posts.asp?TID=25072
Printed Date: 29 March 2026 at 2:09pm
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Search for MULTIPLE words within ONE field??
Posted By: bigchetti
Subject: Search for MULTIPLE words within ONE field??
Date Posted: 28 December 2007 at 9:35am

Hey guys,

I've created my first search page using ASP. Whilst the search function works and data can be obtained, the results will only be successful if the word i try to search is the first word in any entry in one field.

For example...

if i search for BALL

i will get nothing...

however if i search for GLOW BALL i will get a result

i will receive a result...

similarly if i search for GLOW i will get multiple results?
How can i structure the query so that i can search for any of the words within the field and be guaranteed to get a result?

the link for the search form is

http://www.galaxyworldimports.com.au/search.html


Thanks for any help!!

 

a sample of the asp code is listed below:

]<%
 'Open up a connection to our Access Database
 'that stores the customer information.
 Set MyConn = Server.CreateObject("ADODB.Connection")
      MdbFilePath = Server.MapPath("Databases/gal_imports.mdb")
      MyConn.Open "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & MdbFilePath & ";"
      
 'Read in the charactes the user entered
 Dim strProductNameQ
 strProductNameQ = Trim(Request("ProductNameQ"))
 
 'Create our SQL statenebt variable
 Dim strSQL
 strSQL = "SELECT ProductName, ProductPrice FROM Products WHERE ProductName LIKE '" & _
  strProductNameQ & "%' ORDER BY ProductName"
 
 'Create a recordset object instance, and execute the SQL Statement
 Dim objRS
 Set objRS = Server.CreateObject("ADODB.Recordset")
 objRS.Open strSQL, MyConn
 
 'Determine whether or not there are any records in objRS
 If objRS.EOF then
  'No records were returned for the characters entered by the user
  Response.Write "There are no products that contain " & _
   UCase(strProductNameQ)
  
 Else
  'There are one or more products that meet the condition entered by the
  'user. Display these products...
  
  Response.Write "<B>A Listing of Products beginning with " & _
   UCase(strProductNameQ) & ":</B><BR>"
  
  Do While Not objRS.EOF
   Response.Write objRS("ProductName") & " - " & _
     FormatCurrency(objRS("ProductPrice")) & "<BR>"
     
  
   'Move on to the next customer
   objRS.MoveNext
  Loop
 End If
 
 'Clean up our ADO objects
 objRS.Close
 Set objRS = Nothing
 
 MyConn.Close
 Set MyConn = Nothing
 
 %>

 

 




Replies:
Posted By: Scotty32
Date Posted: 28 December 2007 at 12:03pm
if you look at the following (the SQL Statement)

  strSQL = "SELECT ProductName, ProductPrice FROM Products WHERE ProductName LIKE '" & _ 
  strProductNameQ & "%' ORDER BY ProductName"


the part is red (%) is the wildcard, which in this case says find everything that starts with search term.

if you did

  strSQL = "SELECT ProductName, ProductPrice FROM Products WHERE ProductName LIKE '%" & _ 
  strProductNameQ & "%' ORDER BY ProductName"


it will find all instances of the word. so if you searched for ball, it will find it if its the first, last or middle word.

if you delete the % at the end, it will basically do find all records that end in search term.

hope that helps.


-------------
S2H.co.uk - http://www.s2h.co.uk/wwf/" rel="nofollow - WebWiz Mods and Skins

For support on my mods + skins, please use http://www.s2h.co.uk/forum/" rel="nofollow - my forum .


Posted By: bigchetti
Date Posted: 28 December 2007 at 12:06pm
Thanks that works perfectly! Greatly appreciated!



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net