Forum Moderators: open

Message Too Old, No Replies

ASP code to evaluate and return results of 2 colums

         

Darkelve

11:46 am on Jun 7, 2006 (gmt 0)

10+ Year Member



I have some code that searches a column (in an access database)
and returns the results if it matches the query the user has entered (via a picklist of cities in a form).

However, now I have to make it so that it includes municipalities as well...

I think the most straightforward way would be to use two colums, one containing
cities (each cell holds a value here) and another one containing municipalities
(could be empty, or hold the same value as the municipality, e.g. "New York (no municipality) - New York (city)" (just an example). Only this could be a problem if New York is *also* a municipality (and there *are* cases of that for my uses). So maybe it's better to leave the field empty if I'm checking for municipalities?

The code should first search through the "city" column, and list the results, like it does in the code below.
Then, it should search the 'municipality' column and return all the results (probably I'll create a 'divider' row in the html table for this.)

Sorry, I'm not very well-versed in ASP... it's not really my task (I maintain the content of the site; in fact this is my first little 'web app')), but my boss insists on it so I'm trying to figure it out... I hope my explanation is clear enough.


<%
'First you must define the variables you are going to use
Dim accessdb
Dim ConnectionString2
Dim Connection2
Dim strQuery2
Dim RS2
dim city_value
dim dynamicrequest
city_value=Request.QueryString("code")
dynamicrequest=("SELECT * FROM points_of_sale WHERE city='") & (city_value) & ("' ORDER BY address")

If Request.Querystring("code")="" Then Response.Write ("Please choose another value")

'Enter the location of your database
accessdb=Server.MapPath("/scripts/pos.mdb")

'Now we will build the dsn-less connection

ConnectionString2="DRIVER={Microsoft Access Driver (*.mdb)};"
ConnectionString2=ConnectionString2 & "DBQ=" & accessdb

'Create the ADO Connection object set
Set Connection2 = Server.CreateObject("ADODB.Connection")
Connection2.Open ConnectionString2

'Create an SQL query string
strQuery2 = dynamicrequest

' Execute the SQL query string
Set RS2 = Connection2.Execute(strQuery2)

'Show the contents of the table
While Not RS2.EOF
Response.Write ("<tr>")
Response.Write ("<td class='tdemleft'>")
Response.Write RS2("Name") & "&nbsp;"
Response.Write ("</td>")

Response.Write ("<td>")
Response.Write RS2("address") & "&nbsp;"
Response.Write ("</td>")

Response.Write ("<td>")
Response.Write RS2("street_nr") & "&nbsp;"
Response.Write ("</td>")

Response.Write ("<td>")
Response.Write RS2("Bus_nr") & "&nbsp;"
Response.Write ("</td>")

Response.Write ("<td>")
Response.Write RS2("city") & "&nbsp;"
Response.Write ("</td>")

Response.Write ("<td>")
Response.Write RS2("Postalcode") & "&nbsp;"
Response.Write ("</td>")

Response.Write "</tr>"

RS2.MoveNext
Wend
Response.Write ("</table>")

'close the connection
RS2.Close : Connection2.Close
set RS2 = Nothing : set Connection2 = Nothing
%>

Darkelve

2:05 pm on Jun 8, 2006 (gmt 0)

10+ Year Member



Okay, I'm getting some grasp on how I could do this...

Currently, to populate a drop-down list I have:

SQL STATEMENT


SELECT DISTINCT City, Municipality FROM POS

ASP / HTML CODE


'Show the contents of the table
Response.Write ("<select name='code'>")
Response.Write ("<option value=''>Select your city</option>")
While Not RS.EOF
Response.Write("<option value='")
Response.Write RS("City")
Response.Write ("'>")
Response.Write RS("City")
Response.Write ("</option>")
RS.MoveNext
Wend
Response.Write ("</select>")
Response.Write(" <input type='submit' value='zoeken' />")
Response.Write ("</form>")
Response.Write ("<br /><br />")

Is there any way in the SQL statement so I can:
- select *all* of the values of the two colums,
- ignore the value of the second column if it's identical to the first
- order them alphabetically in the drop-down list