Form to SQL statement

Form to SQL statement

am 29.09.2005 13:58:03 von KiwiNETukgeocitiescomharoonnet2002

Hello folks, I wonder if you could solve this problem for me. I have some
code here that's meant to take data from drop-down boxes, each containing the
ANY value, enabling users to select non-particular clauses for database
search. I manage to get records returned with one ANY selected, but with
more than one none are returned. Here is the form code:








Primary Dish Type?
Spice?


Dish Content?



and here is the code page that's meant to deal with the form page:







<%

Dim Connection 'Here we declare our variable that will hold our new object
Set Connection=Server.CreateObject("ADODB.Connection")
Dim sConnString 'Here we declare our variable that will hold the
connection string
sConnString="PROVIDER=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" &
Server.MapPath("MENU TABLE_NEW.MDB")

Connection.Open sConnString

Dim Recordset 'Here we declare our variable that will hold our new object
Set Recordset=Server.CreateObject("ADODB.Recordset")

primary=Request.Form("primary")
spice=Request.Form("spice")
dish_content=Request.Form("dish_content")

Dim SQL(3) 'Here we declare our variable that will hold the SQL statement
Rem SQL=Request.Form("txtData")

SQL(0)="Select Name,Description,Price,Availability From Menu Where "

If Len(Trim(primary))=0 Then
SQL(1)="Spice = '" & spice & "' And Type = '" & dish_content & "'"
ElseIf Len(Trim(spice))=0 Then
SQL(1)="Primary_Dish_Type = '" & primary & "' And Type = '" & dish_content
& "'"
ElseIf Len(Trim(dish_content))=0 Then
SQL(1)="Primary_Dish_Type = '" & primary & "' And Spice = '" & spice & "'"
ElseIf (Len(Trim(primary)) And Len(Trim(spice)))=0 Then
SQL(1)="Type = '" & dish_content & "'"
ElseIf (Len(Trim(spice)) And Len(Trim(dish_content)))=0 Then
SQL(1)="Primary_Dish_Type = '" & primary & "'"
ElseIf (Len(Trim(primary)) And Len(Trim(dish_content)))=0 Then
SQL(1)="Spice = '" & spice & "'"
Else SQL(1)="Primary_Dish_Type = '" & primary & "' And Spice = '" & spice &
"' And Type = '" & dish_content & "'"
End If

SQL(2)=" Order By Name"

Rem Response.Write SQL
Rem Response.Flush
Recordset.Open SQL(0) & SQL(1) & SQL(2), Connection
Rem Recordset.Open SQL, Connection

%>
<% Response.Write("Primary Dish Type: " & primary)
%>
<% Response.Write("Spice: " & spice)
%>
<% Response.Write("Dish Content: " & dish_content)

%>


<% Do While NOT Recordset.Eof 'i.e. carry on looping through while there
are records
If Recordset("Availability")=True Then %>







<% End If
Recordset.MoveNext 'move on to the next record
Loop %>
<% Response.Write Recordset("Name") %> <% Response.Write Recordset("Price")
%>
<% Response.Write
Recordset("Description") %>


<%
Recordset.Close
Set Recordset=Nothing
Connection.Close
Set Connection=Nothing
%>



Look at the If conditioning in the code above. What am I missing? Thanks.

Re: Form to SQL statement

am 05.10.2005 08:20:34 von SteveB

Perhaps this may hopefully give you some ideas:

Classic ASP Design Tips - Search Criteria on Multiple Fields
http://www.bullschmidt.com/devtip-searchcriteria.asp

Example on a page receiving a post of a SQL statement based on two
listboxes (Rep and Customer) that might each have a value of "" (i.e.
blank) to show all with no criteria restrictions.

' Get posted form vars.
Rep = Request.Form("Rep")
Customer = Request.Form("Customer")

' Set strSQL.
strSQL = "SELECT * FROM MyTable WHERE (1=1)"
If Rep <> "" Then
strSQL = strSQL & " AND (Rep='" & Rep & "')"
End If
If Customer <> "" Then
strSQL = strSQL & " AND (Customer='" & Customer & "')"
End If

Response.Write "strSQLWhere: " & strSQLWhere

And the (1=1) above is there as a placeholder (which doesn't affect the
results since it is always true) as the SQL statement possibly may use
AND with criteria after it or possibly the SQL statement may not have
anything after it. For example a SQL statement without the (1=1) like
this would not work: SELECT * FROM MyTable WHERE AND Customer='API'

Best regards,
J. Paul Schmidt, Freelance Web and Database Developer
http://www.Bullschmidt.com
Classic ASP Design Tips, ASP Web Database Sample

*** Sent via Developersdex http://www.developersdex.com ***