Wrox Home  
Search P2P Archive for: Go

  Return to Index  

asp_databases thread: search database for multiple AND fields/sql statement


Message #1 by "d quintal" <quintad@t...> on Fri, 24 Aug 2001 15:58:06
Here is my dilemma:

Any help would be greatly appreciated as I am a ASP newbie of sorts.



I have a page that searches an Access database using multiple dropdown 

fields which are each populated by their respective fields in the database.

Right now I have this working so that when a user selects from these 

dropdowns  the database returns all entries matching one or more of their 

criteria.



What I am having trouble with is the version that will return only entries 

that fulfill ALL of the criteria. I feel like I almost have it but cannot 

get the:

city AND state AND MATopic AND Country AND Topic UNLESS any of the above 

are =?n? ( unselected from the dropdown)



Here is the code that is working for the first scenario:

Thank you!

Delani





'Pass the form info into the variables

	strCity=request.form ("City")

	strState=request.form ("State")

	strMATopic=request.form ("MATopic")	

	strCountry=request.form ("Country")

	strTopic=request.form ("Topic")



	

	If strCity <> "N" Then

	strsql= strsql & " WHERE city='" & strCity & "'"

	alreadywhere="1"

End If



If strState <> "N" Then

	If alreadywhere <> "1" Then

		strsql = strsql & " AND state='" & strState & "'"

	Else

		strsql= strsql & " WHERE state='" & strState & "'"

		alreadywhere="1"

	End If

End If



If strMATopic <> "N" Then

	If alreadywhere <> "1" Then

		strsql = strql & " AND MATopic='" & strMATopic & "'"

	Else

		strsql= strsql & " WHERE MATopic='" & strMATopic & "'"

		alreadywhere="1"

	End If

End If



If strCountry <> "N" Then

	If alreadywhere <> "1" Then

		strsql = strsql & " AND country='" & strCountry & "'"

	Else

		strsql= strsql & " WHERE country='" & strCountry & "'"

		alreadywhere="1"

	End If

End If



If strTopic <> "N" Then

	If alreadywhere <> "1" Then

		strsql = strsql & " AND [Topic 1] = '" & strtopic & "'"

	Else

		strsql= strsql & " WHERE [Topic 1] = '" & strtopic & "'"

		alreadywhere="1"

	End If

End If



AND HERE IS THE SQL statement to pull the corresponding fields out of the 

database:



strSQL= "SELECT * FROM tblAttyReferrals WHERE  (City = " & "'" & strcity 

& "'" & ") OR (State = " & "'" & strState & "'" & ") OR (MATopic = " & "'" 

& strMATopic & "'" & ") OR (country = " & "'" & strcountry & "'" & ") OR  

([Topic 1] = " & "'" & strtopic & "'" & ")" & _ 

	" ORDER by SName asc"


  Return to Index