Form data to SQL statement

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:

<form method="post" action="OnLineShop.asp">
<table>
<td>Primary Dish Type?
<select name="primary">
<option value="" selected>ANY</option>
<option value="Starter">Starter</option>
<option value="Main Course">Main Course</option>
<option value="Accompaniments">Accompaniments</option>
</select></td>
<td>Spice?
<select name="spice">
<option value="" selected>ANY</option>
<option value="None">None</option>
<option value="Mild">Mild</option>
<option value="Medium">Medium</option>
<option value="Medium+">Medium+</option>
<option value="Medium or Hot">Medium or Hot</option>
<option value="Hot">Hot</option>
<option value="Very Hot">Very Hot</option>
</select>
<!-- <input name="txtData" type="text" id="txtData"> -->
</td>
<td>Dish Content?
<select name="dish_content">
<option value="" selected>ANY</option>
<option value="Vegetarian">Vegetarian</option>
<option value="Vegetable">Containing Vegetables</option>
<option value="Mushroom">Mushroom</option>
<option value="Prawn">Prawn</option>
<option value="King Prawn">King Prawn</option>
<option value="Chicken">Chicken</option>
<option value="Lamb">Lamb (general)</option>
<option value="Keema">Keema</option>
<option value="Mixed">Mixed</option>
<option value="Fish">Fish</option>
<option value="Dairy">Dairy</option>
<option value="Chicken Tikka">Chicken Tikka</option>
<option value="Lamb Tikka">Lamb Tikka</option>
<option value="Tandoori King Prawn">Tandoori King Prawn</option>
</select></td>
<td><input type="submit" name="Submit" value="Go"></td>
</p>
</table>
</form>

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

<head>
<link rel="stylesheet" type="text/css" href="monsoon.css">
</head>

<html>
<body>
<%

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

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

%>

<table cols="5" border="0">
<% Do While NOT Recordset.Eof 'i.e. carry on looping through while there
are records
If Recordset("Availability")=True Then %>
<tr>
<td class="name_and_price"><% Response.Write Recordset("Name") %></td>
<td class="name_and_price"><% Response.Write Recordset("Price")
%></td>
</tr>
<tr>
<td class="description" width="50%"><% Response.Write
Recordset("Description") %></td>
</tr>
<% End If
Recordset.MoveNext 'move on to the next record
Loop %>
</table>

<%
Recordset.Close
Set Recordset=Nothing
Connection.Close
Set Connection=Nothing
%>
</body>
</html>

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

I am using IIS version 5.0 and am on local testing server.
KiwiNETukgeocitiescom [ Do, 29 September 2005 14:00 ] [ ID #990056 ]

RE: Form data to SQL statement

Here's what keeps being passed to the OnLineShop for some weird reason:

Select Name,Description,Price,Availability From Menu Where Primary_Dish_Type
= 'Accompaniments' And Type = '' Order By Name

when I just select 'Accompaniments' on the form and leave the other two as
'ANY'. Please tell me where I have made the typo, in the code from my last
post in the other thread. Thanks.
KiwiNETukgeocitiescom [ Do, 29 September 2005 15:34 ] [ ID #990058 ]

Re: Form data to SQL statement

"KiwiNET uk.geocities.com/haroonnet2002/"
<KiwiNETukgeocitiescomharoonnet2002 [at] discussions.microsoft.com> wrote in
message news:3DCB7177-D749-477B-8859-EF1214D26093 [at] microsoft.com...
> Here's what keeps being passed to the OnLineShop for some weird reason:
>
> Select Name,Description,Price,Availability From Menu Where
Primary_Dish_Type
> = 'Accompaniments' And Type = '' Order By Name
>
> when I just select 'Accompaniments' on the form and leave the other two as
> 'ANY'. Please tell me where I have made the typo, in the code from my
last
> post in the other thread. Thanks.

1) "Name" and "Type" may be reserved words and should be enclosed in
brackets.

2) "Type" is being tested for spaces -- is that what you want?

3) Try the following; watch for word-wrap.

Dim NAM(2)
NAM(0) = "Primary_Dish_Type"
NAM(1) = "Spice"
NAM(2) = "Type"
Dim VAL(2)
VAL(0) = Request.Form("primary")
VAL(1) = Request.Form("spice")
VAL(2) = Request.Form("dish_content")
Dim SQL(2)
SQL(0)="SELECT [Name],Description,Price,Availability FROM Menu WHERE "
Dim i
For i = 0 To UBound(NAM)
If Len(Trim(VAL(i))) <> 0 Then
If SQL(1) <> "" Then SQL(1) = SQL(1) & " AND "
SQL(1) = "[" & NAM(i) & "] = '" & VAL(i) & "'"
End If
Next
SQL(2)=" Order By Name"

This includes in the WHERE test those fields that have a value.

Note 1: I "Dim" all my variables as I always use "Option Explicit".

Note 2: I capitalized the SQL words SELECT, FROM, WHERE, AND.

The above is untested; let me know if there's a problem.
McKirahan [ Do, 29 September 2005 16:03 ] [ ID #990061 ]

Re: Form data to SQL statement

Perhaps this may hopefully give you some ideas:

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

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 ***
SteveB [ Do, 29 September 2005 23:59 ] [ ID #990066 ]

Re: Form data to SQL statement

McKirahan, I'll get back to you in a bit...

> The above is untested; let me know if there's a problem.
>
>
>
KiwiNETukgeocitiescom [ Fr, 30 September 2005 11:52 ] [ ID #991775 ]

Re: Form data to SQL statement

It's about checking for non-null data really - the part where it checks for
null data, when one ANY is selected works fine. Where more than one ANY is
selected, I don't know where that rogue quotation mark comes from in the SQL
statement being passed to the OnLineShop.asp file. I'll try what you
suggested, thanks...

"Bullschmidt" wrote:

> Perhaps this may hopefully give you some ideas:
>
> Classic ASP Design Tips - Search Criteria on Multiple Fields
> http://www.bullschmidt.com/devtip-searchcriteria.asp
>
> 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 ***
>
KiwiNETukgeocitiescom [ Fr, 30 September 2005 11:55 ] [ ID #991776 ]

RE: Form data to SQL statement

OK folks, thanks for all the help. I have now solved the problem. 'If'
conditioning didn't work for me, but I used Functions embedded within SQL
statements:

SQL="Select Name,Description,Price,Availability From Menu " &
PrimaryNotNull(primary) & SecondaryNotNull(secondary) & SpiceNotNull(spice) &
DishContentNotNull(dish_content)

Function PrimaryNotNull(primary)
If Len(Trim(primary))=0 Then
appropriate=""
Else
appropriate="Where Primary_Dish_Type = '" & primary & "'"
End If
PrimaryNotNull = appropriate
End Function

Function SpiceNotNull(spice)
If Len(Trim(spice))=0 Then
appropriate=""
Else
appropriate=" And Spice = '" & spice & "'"
End If
SpiceNotNull = appropriate
End Function

Function DishContentNotNull(dish_content)
If Len(Trim(dish_content))=0 Then
appropriate=""
Else
appropriate=" And Content = '" & dish_content & "'"
End If
DishContentNotNull = appropriate
End Function

The correct statements are being passed to the SQL now. The records are
being returned without error.
KiwiNETukgeocitiescom [ So, 02 Oktober 2005 22:17 ] [ ID #993739 ]
Webserver » microsoft.public.inetserver.asp.db » Form data to SQL statement

Vorheriges Thema: Notification of User Entering New Record in Remote Database
Nächstes Thema: How to add a button to Modify, delete or add a new record to this code