code,lets,visitors,your,site,perform,complex,
Quick Search for:  in language:    
code,lets,visitors,your,site,perform,complex,
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
ASP/ VbScript Stats

 Code: 118,709 lines
 Jobs: 148 postings

 
Sponsored by:

 

You are in:

 
Login


 

 


Latest Code Ticker for ASP/ VbScript.
Getting a random record from a table
By Serkan YOGURAN on 6/11


cool circle and line drawer
By john sheridan on 6/10


Click here to see a screenshot of this code!A Site Manager with Free Upload Functions (without payable components)
By Francisco J Riquelme on 6/10

(Screen Shot)

Simple Calculator
By Alistair Rodrigues on 6/9


Nutty Password
By jason mulryan on 6/9


Enviar email HTML ( send an email html )
By Javi P. D. on 6/9


Banner (rotacion de banners)
By Javi P. D. on 6/9


Paginar ( Paging )
By Javi P. D. on 6/9


Click here to see a screenshot of this code!Online photo catalogue VBScript
By Ivan Loire on 6/8

(Screen Shot)

Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!





Affiliate Sites



 
 
   

Advanced SQL Query Builder

Print
Email
 
VB icon
Submitted on: 8/3/2000 10:57:26 PM
By: Lewis Moten  
Level: Intermediate
User Rating: By 5 Users
Compatibility:ASP (Active Server Pages)

Users have accessed this code 20430 times.
 

(About the author)
 
     This code lets visitors to your site perform complex queries. Users may choose if specific words (or phrases) must or must not match - or if they are optional (default).

 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!

    '**************************************
    ' for :Advanced SQL Query Builder
    '**************************************
    Copyright (C) 1999, Lewis Moten. All rights reserved. Any modifications must be sent To me so that I may review and add them to the procedure as I feel fit.
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for langauges that allow it) freely and with no charge.   
2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.

    '**************************************
    ' Name: Advanced SQL Query Builder
    ' Description:This code lets visitors to
    '     your site perform complex queries. Users
    '     may choose if specific words (or phrases
    '     ) must or must not match - or if they ar
    '     e optional (default).
    ' By: Lewis Moten
    '
    ' Inputs:asFieldsAry - An array of field
    '     names in database to search.
    asKeywords - The actual query that the user types To query the database.
    Keyword Search Parameters
    To find fields that may have a word in them
    	OR roger
    	| roger
    	roger
    To find fields that must match a word
    	AND roger
    	+ roger
    	& roger
    To find fields that must Not match a word
    	Not roger
    	- roger
    Also use phrases
    	+"rogers dog" -cat
    	+(rogers dog)
    '
    ' Returns:Returns just the SQL arguments
    '     within a group that are to be places aft
    '     er the WHERE Clause.
    '
    ' Assumes:It is assumed that the user kn
    '     ows how to build an array of field names
    '     and understand that syntax of SQL querie
    '     s along with how to connect to databases
    '     . This procedure has only been tested wi
    '     th SQL Servers and Access databases.
    '
    ' Side Effects:This function uses the Re
    '     gExp object that was introduced in vbScr
    '     ipt 5.0 that came out with Internet Expl
    '     orer 5.0. The vbScript can be installed 
    '     without installing Internet Explorer by 
    '     going to the subdirectory "Scripting" on
    '     the microsoft site.
    '
    'This code is copyrighted and has    ' limited warranties.Please see http://w
    '     ww.Planet-Source-Code.com/xq/ASP/txtCode
    '     Id.6297/lngWId.4/qx/vb/scripts/ShowCode.
    '     htm    'for details.    '**************************************
    
    function BuildQuery(ByRef asFieldAry, ByVal asKeyWords)
    	Dim loRegExp			' Regular Expression Object (requires vbScript 5.0)
    	Dim loRequiredWords		' Words that MUST match within a search
    	Dim loUnwantedWords		' Words that MUST Not match within a search
    	Dim loOptionalWords		' Words that AT LEAST ONE must match
    	Dim lsSQL				' Arguments of SQL query that is returned (WHERE __Arguments___)
    	Dim lnIndex				' Index of an array
    	Dim lsKeyword			' Keyword or Phrase being worked With
    	' An Error may occur within your script
    	' Even if you Do Not call this function
    	' if you Do Not have vbScript 5.0 installed on your server
    	' because of the Next line.
    	' Create regular Expression
    	Set loRegExp = New RegExp
    	' Match more Then once
    	loRegExp.Global = True
    	' Every letter is created equal (uppercase-lowercase = same)
    	loRegExp.IgnoreCase = True
    	' pull out keywords and phrases that MUST match within a search
    	loRegExp.Pattern = "((AND|[+&])\s*[\(\[\{""].*[\)\]\}""])|((AND\s|[+&])\s*\b[-\w']+\b)"
    	Set loRequiredWords = loRegExp.Execute(asKeywords)
    	asKeywords = loRegExp.Replace(asKeywords, "")
    	' pull out keywords and phrases that MUST Not match within a search
    	loRegExp.Pattern = "(((NOT|[-])\s*)?[\(\[\{""].*[\)\]\}""])|(((NOT\s+|[-])\s*)\b[-\w']+\b)"
    	Set loUnwantedWords = loRegExp.Execute(asKeywords)
    	asKeywords = loRegExp.Replace(asKeywords, "")
    	' pull out keywords and phrases that must have AT LEAST ONE match within a search
    	loRegExp.Pattern = "(((OR|[|])\s*)?[\(\[\{""].*[\)\]\}""])|(((OR\s+|[|])\s*)?\b[-\w']+\b)"
    	Set loOptionalWords = loRegExp.Execute(asKeywords)
    	asKeywords = loRegExp.Replace(asKeywords, "")
    	' if at least 1 required word was found
    	if Not loRequiredWords.Count = 0 Then
    		' REQUIRED
    		' Open a new group
    		lsSQL = lsSQL & "("
    		' Loop through Each keyword/phrase
    		For lnIndex = 0 To loRequiredWords.Count - 1
    			' Pull the keyword out
    			lsKeyword = loRequiredWords.Item(lnIndex).Value
    			' Strip Boolean language
    			loRegExp.Pattern = "^(AND|[+&])\s*"
    			lsKeyword = loRegExp.Replace(lsKeyword, "")
    			loRegExp.Pattern = "[()""\[\]{}]"
    			lsKeyword = loRegExp.Replace(lsKeyword, "")
    			' Double Quote Keyword
    			lsKeyword = Replace(lsKeyword, "'", "''")
    			' if we are Not working With the first keyword
    			if Not lnIndex = 0 Then
    				' append logic before the keyword
    				lsSQL = lsSQL & " AND "
    		 	End if ' Not lnIndex = 0
    		 	' Append SQL To search For the keyword within all searchable fields
    			lsSQL = lsSQL & "(" & Join(asFieldAry, " LIKE '%" & lsKeyword & "%' OR ") & " LIKE '%" & lsKeyword & "%')"
    		Next ' lnIndex
    		' Close the group
    		lsSQL = lsSQL & ")"
    	End if ' Not loRequiredWords.Count = 0
    	' if at least 1 optional word was found
    	if Not loOptionalWords.Count = 0 Then
    		' OPTIONAL
    		' if the SQL query is Not yet defined
    		if lsSQL = "" Then
    			' Open a new group
    			lsSQL = "("
    		' Else SQL query has content
    		Else
    			' Append logic before the group
    			lsSQL = lsSQL & " AND ("
    		End if ' lsSQL = ""
    		' Loop through Each keyword/phrase
    		For lnIndex = 0 To loOptionalWords.Count - 1
    			' Pull the keyword out
    			lsKeyword = loOptionalWords.Item(lnIndex).Value
    			' Strip Boolean Language
    			loRegExp.Pattern = "^(OR|[|])\s*"
    			lsKeyword = loRegExp.Replace(lsKeyword, "")
    			loRegExp.Pattern = "[()""\[\]{}]"
    			lsKeyword = loRegExp.Replace(lsKeyword, "")
    			' Double Quote the keyword
    			lsKeyword = Replace(lsKeyword, "'", "''")
    			' if we are Not working With the first keyword
    			if Not lnIndex = 0 Then
    				' Append Logic before the keyword search
    				lsSQL = lsSQL & " OR "
    			End if ' Not lnIndex = 0
    			' Append SQL To search For the keyword within all searchable fields
    			lsSQL = lsSQL & "(" & Join(asFieldAry, " LIKE '%" & lsKeyword & "%' OR ") & " LIKE '%" & lsKeyword & "%')"
    		Next ' lnIndex
    		' Close the group
    		lsSQL = lsSQL & ")"
    	End if ' Not loOptionalWords.Count = 0
    	' if at least 1 Unwanted word was found
    	if Not loUnwantedWords.Count = 0 Then
    		' UNWANTED
    		' if the SQL query is Not yet defined
    		if lsSQL = "" Then
    			' Open a new group
    			lsSQL = "("
    		' Else SQL query has content
    		Else
    			' Append logic before the group
    			lsSQL = lsSQL & " AND Not ("
    		End if ' lsSQL = ""
    		' Loop through Each keyword/phrase
    		For lnIndex = 0 To loUnwantedWords.Count - 1
    			' Pull the keyword out
    			lsKeyword = loUnWantedWords.Item(lnIndex).Value
    			' Strip Boolean Language
    			loRegExp.Pattern = "^(NOT|[-])\s*"
    			lsKeyword = loRegExp.Replace(lsKeyword, "")
    			loRegExp.Pattern = "[()""\[\]{}]"
    			lsKeyword = loRegExp.Replace(lsKeyword, "")
    			' Double Quote the keyword
    			lsKeyword = Replace(lsKeyword, "'", "''")
    			' if we are Not working With the first keyword
    			if Not lnIndex = 0 Then
    				' Append Logic before the keyword search
    				lsSQL = lsSQL & " OR "
    			End if ' Not lnIndex = 0
    			' Append SQL To search For the keyword within all searchable fields
    			lsSQL = lsSQL & "(" & Join(asFieldAry, " LIKE '%" & lsKeyword & "%' OR ") & " LIKE '%" & lsKeyword & "%')"
    		Next ' lnIndex
    		' Close the group
    		lsSQL = lsSQL & ")"
    	End if ' Not loUnwantedWords.Count = 0
    	' if arguments were created
    	if Not lsSQL = "" Then
    		' Encapsilate Arguments as a group
    		' In Case other aguments are To be appended
    		lsSQL = "(" & lsSQL & ")"
    	End if ' Not lsSQL = ""
    	' Return the results
    	BuildQuery = lsSQL
    End function ' BuildQuery


Other 82 submission(s) by this author

 

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
Your Vote!

What do you think of this code(in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments
8/4/2000 4:09:15 PM:frihani
You seem to have an excellent grasp on 
regular expressions. This code 
demonstrates the power and usefulness 
of RegExp, hopefully others will find 
similar uses. 
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/4/2000 9:52:34 PM:Lewis Moten
Thanks.  I read a good O'Reilly book 
about them call Mastering Regular 
Expressions.  I still have a few other 
tricks up my sleeves with them.  Most 
of the time I use them to export data 
from standard text-based forms on the 
internet
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/18/2001 10:20:28 PM:ap
hi Lewis,
Can i form where clause , 
which looks like following , using your 
code?
1. A>10 and (b > (c or d))
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | ASP/ VbScript Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.