PDA

View Full Version : Dropdown List Search Help?


awayne
09-28-2002, 08:07 AM
I have an excel file with a listing of customers... The columns are Customer, telephone, address, city, state, zip I would love to be able to search by City state or zip... But I beleive I don't have the know how to do this. I do know of where I can get the listing of all zip codes to be able to search in a range, but I want to make sure that I could do this first before I get it. If someone could, please help me to have a dropdown list of all states and when you pick for example Ohio, you would get all the customers from ohio displayed on the page. Now, I would also like to have an admin page so that you can go in and change any of the customers already in there or add another one. All help is appreciated with this.

Thanks
Wayne

whammy
09-28-2002, 05:10 PM
Here's a very basic example of one way to do a search feature like this (assuming you first imported the excel file into an Access database in a table called "table_name":

"search1.asp":

<% @Language="VBScript" %>
<%
Dim Found
Found = Request.QueryString("Found")
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<title>Database Search</title>
</head>
<body>
<% If Found = "0" Then Response.Write("<font color="red">No records were found matching your criteria.</font><br />") %>
<form name="form1" method="post" action="search2.asp">
Select all records where:
<select name="SearchOption1">
<option value="Customer">Customer</option>
<option value="Telephone">Telephone</option>
<option value="Address">Address</option>
<option value="City">City</option>
<option value="State">State</option>
<option value="Zip">Zip</option>
</select>
<select name="SearchOption2">
<option value="Equals">Equals</option>
<option value="Contains">Contains</option>
</select>
<input type="text" name="SearchOption3">
</form>
</body>
</html>


"search2.asp":

<% @Language="VBScript" %>
<%
Dim SearchOption1, SearchOption2, SearchOption3
Dim SQLStart, SQLEnd

SearchOption1 = Request.Form("SearchOption1")
SearchOption2 = Request.Form("SearchOption2")
SearchOption3 = Request.Form("SearchOption3")

Select Case SearchOption2
Case "EQUALS"
SQLStart = " = '"
SQLEnd = "' "
Case "CONTAINS"
SQLStart = " LIKE '%"
SQLEnd = "%' "
End Select

Set Conn = Server.CreateObject("ADODB.Connection")
sConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="
sMapPath = Server.MapPath("\")
sMapPath = Mid(sMapPath, 1, InStrRev(sMapPath,"\")-1) & "\database\databasename.mdb;" & _
"Persist Security Info=False;"
sConnString = sConnString & sMapPath
Conn.Open sConnString

SELECTQUERY = "SELECT * FROM table_name WHERE " & SearchOption1 & SQLStart & Replace(SearchOption3,"'","''") & SQLEnd & " ORDER BY " & SearchOption1
'Response.Write(SELECTQUERY) : Response.End
'If any SQL syntax errors are thrown, uncomment the line above!
Set rs = Conn.Execute(SELECTQUERY)

If NOT rs.EOF Then

Do While NOT rs.EOF
Response.Write("<p>" & _
rs("Customer") & "<br />" & _
rs("Telephone") & "<br />" & _
rs("Address") & "<br />" & _
rs("City") & "<br />" & _
rs("State") & "<br />" & _
rs("Zip") & _
"</p>")
rs.MoveNext
Loop

Set rs = Nothing
Conn.Close
Set Conn = Nothing
Else
Set rs = Nothing
Conn.Close
Set Conn = Nothing

Response.Redirect("search1.asp?Found=0")
End If
%>


I wrote that completely from scratch out of my head, and I haven't tested it at all - so it may contain errors - but hopefully not too many. ;)

awayne
09-28-2002, 07:02 PM
Thanks for the response whammy!! I guess I might have wrote my question wrong. I originally was only going to have a drop down list of the states and a submit button and it would pull all the data that equals that state. But I would like that have a way for you to select either state from a drop down list, have a text input for zip code and then a range in miles in a dropdown box. I am not very familiar with asp, but I have used php before alittle bit. Again, all help is appreciated.

Thanks
Wayne

whammy
09-28-2002, 07:29 PM
You're on your own for the "range of miles" - however to select a state (or zip), it works the same way:


<select name="State">
<option value="">------------------------------</option>
<option value="AL"<% If State = "AL" Then Response.Write(" selected=""selected""") %>>Alabama</option>
<option value="AK"<% If State = "AK" Then Response.Write(" selected=""selected""") %>>Alaska</option>
<option value="AZ"<% If State = "AZ" Then Response.Write(" selected=""selected""") %>>Arizona</option>
<option value="AR"<% If State = "AR" Then Response.Write(" selected=""selected""") %>>Arkansas</option>
<option value="CA"<% If State = "CA" Then Response.Write(" selected=""selected""") %>>California</option>
<option value="CO"<% If State = "CO" Then Response.Write(" selected=""selected""") %>>Colorado</option>
<option value="CT"<% If State = "CT" Then Response.Write(" selected=""selected""") %>>Connecticut</option>
<option value="DC"<% If State = "DC" Then Response.Write(" selected=""selected""") %>>District of Columbia</option>
<option value="DE"<% If State = "DE" Then Response.Write(" selected=""selected""") %>>Delaware</option>
<option value="FL"<% If State = "FL" Then Response.Write(" selected=""selected""") %>>Florida</option>
<option value="GA"<% If State = "GA" Then Response.Write(" selected=""selected""") %>>Georgia</option>
<option value="HI"<% If State = "HI" Then Response.Write(" selected=""selected""") %>>Hawaii</option>
<option value="ID"<% If State = "ID" Then Response.Write(" selected=""selected""") %>>Idaho</option>
<option value="IL"<% If State = "IL" Then Response.Write(" selected=""selected""") %>>Illinois</option>
<option value="IN"<% If State = "IN" Then Response.Write(" selected=""selected""") %>>Indiana</option>
<option value="IA"<% If State = "IA" Then Response.Write(" selected=""selected""") %>>Iowa</option>
<option value="KS"<% If State = "KS" Then Response.Write(" selected=""selected""") %>>Kansas</option>
<option value="KY"<% If State = "KY" Then Response.Write(" selected=""selected""") %>>Kentucky</option>
<option value="LA"<% If State = "LA" Then Response.Write(" selected=""selected""") %>>Louisiana</option>
<option value="ME"<% If State = "ME" Then Response.Write(" selected=""selected""") %>>Maine</option>
<option value="MD"<% If State = "MD" Then Response.Write(" selected=""selected""") %>>Maryland</option>
<option value="MA"<% If State = "MA" Then Response.Write(" selected=""selected""") %>>Massachusetts</option>
<option value="MI"<% If State = "MI" Then Response.Write(" selected=""selected""") %>>Michigan</option>
<option value="MN"<% If State = "MN" Then Response.Write(" selected=""selected""") %>>Minnesota</option>
<option value="MS"<% If State = "MS" Then Response.Write(" selected=""selected""") %>>Mississippi</option>
<option value="MO"<% If State = "MO" Then Response.Write(" selected=""selected""") %>>Missouri</option>
<option value="MT"<% If State = "MT" Then Response.Write(" selected=""selected""") %>>Montana</option>
<option value="NE"<% If State = "NE" Then Response.Write(" selected=""selected""") %>>Nebraska</option>
<option value="NH"<% If State = "NH" Then Response.Write(" selected=""selected""") %>>New Hampshire</option>
<option value="NJ"<% If State = "NJ" Then Response.Write(" selected=""selected""") %>>New Jersey</option>
<option value="NM"<% If State = "NM" Then Response.Write(" selected=""selected""") %>>New Mexico</option>
<option value="NY"<% If State = "NY" Then Response.Write(" selected=""selected""") %>>New York</option>
<option value="NV"<% If State = "NV" Then Response.Write(" selected=""selected""") %>>Nevada</option>
<option value="NC"<% If State = "NC" Then Response.Write(" selected=""selected""") %>>North Carolina</option>
<option value="ND"<% If State = "ND" Then Response.Write(" selected=""selected""") %>>North Dakota</option>
<option value="OH"<% If State = "OH" Then Response.Write(" selected=""selected""") %>>Ohio</option>
<option value="OK"<% If State = "OK" Then Response.Write(" selected=""selected""") %>>Oklahoma</option>
<option value="OR"<% If State = "OR" Then Response.Write(" selected=""selected""") %>>Oregon</option>
<option value="PA"<% If State = "PA" Then Response.Write(" selected=""selected""") %>>Pennsylvania</option>
<option value="RI"<% If State = "RI" Then Response.Write(" selected=""selected""") %>>Rhode Island</option>
<option value="SC"<% If State = "SC" Then Response.Write(" selected=""selected""") %>>South Carolina</option>
<option value="SD"<% If State = "SD" Then Response.Write(" selected=""selected""") %>>South Dakota</option>
<option value="TN"<% If State = "TN" Then Response.Write(" selected=""selected""") %>>Tennessee</option>
<option value="TX"<% If State = "TX" Then Response.Write(" selected=""selected""") %>>Texas</option>
<option value="UT"<% If State = "UT" Then Response.Write(" selected=""selected""") %>>Utah</option>
<option value="VT"<% If State = "VT" Then Response.Write(" selected=""selected""") %>>Vermont</option>
<option value="VA"<% If State = "VA" Then Response.Write(" selected=""selected""") %>>Virginia</option>
<option value="WA"<% If State = "WA" Then Response.Write(" selected=""selected""") %>>Washington</option>
<option value="WV"<% If State = "WV" Then Response.Write(" selected=""selected""") %>>West Virginia</option>
<option value="WI"<% If State = "WI" Then Response.Write(" selected=""selected""") %>>Wisconsin</option>
<option value="WY"<% If State = "WY" Then Response.Write(" selected=""selected""") %>>Wyoming</option>
<option value="">------------------------------</option>
<option value="AA"<% If State = "AA" Then Response.Write(" selected=""selected""") %>>Armed Forces Americas AA</option>
<option value="AP"<% If State = "AP" Then Response.Write(" selected=""selected""") %>>Armed Forces Pacific AP</option>
<option value="AE"<% If State = "AE" Then Response.Write(" selected=""selected""") %>>Armed Forces Other AE</option>
<option value="">------------------------------</option>
<option value="AS"<% If State = "AS" Then Response.Write(" selected=""selected""") %>>American Samoa</option>
<option value="FM"<% If State = "FM" Then Response.Write(" selected=""selected""") %>>Federated States Of Micronesia</option>
<option value="GU"<% If State = "GU" Then Response.Write(" selected=""selected""") %>>Guam</option>
<option value="MH"<% If State = "MH" Then Response.Write(" selected=""selected""") %>>Marshall Islands</option>
<option value="MP"<% If State = "MP" Then Response.Write(" selected=""selected""") %>>Northern Mariana Islands</option>
<option value="PW"<% If State = "PW" Then Response.Write(" selected=""selected""") %>>Palau</option>
<option value="PR"<% If State = "PR" Then Response.Write(" selected=""selected""") %>>Puerto Rico</option>
<option value="VI"<% If State = "VI" Then Response.Write(" selected=""selected""") %>>Virgin Islands</option>
</select>


and on the next page:

"SELECT * FROM tablename WHERE State = '" & State & "'"

awayne
09-29-2002, 07:35 AM
OK, maybe I should say I have no idea what I am doing :) I have tried to look over what you have replied with and I just can't figure it out. Is there any way you can explain better how this all works? To make it simple, all I want is a dropdown menu with all us states listed and a submit button to bring back on the next page only those listings in that state. The only other thing I would like is to have a admin area that you could login to and change any of the database entries or add another one. Thanks again for all your help. Once I have an understanding of this, I can mover forward with my other ideas.

Thanks,
Wayne

whammy
09-29-2002, 04:48 PM
Well, I can't really teach you ASP free of charge (as this is basic ASP stuff)... this forum is for help, not lessons! However, there's a pretty good ASP/database tutorial here (http://hotwired.lycos.com/webmonkey/02/35/index4a.html?tw=backend) that should get you going. They even provide a free blank .mdb file, and a nifty query tool to create/query tables (which I like so much the way it is I used it on my website with some VERY slight modifications!). :)

You might also want to check out:

http://www.liquidrage.com
http://www.haneng.com

Basically you just post the form to another page and get the values with...

Request.Form("variablename")

...then you use a SQL statement to retrieve the appropriate values from the database. If you go through that first tutorial you will understand. :)

awayne
09-29-2002, 08:58 PM
I was trying to teach myself asp and I got this error:

Microsoft OLE DB Provider for ODBC Drivers error '80040e09'

[Microsoft][ODBC Microsoft Access Driver] Cannot modify the design of table 'bandits'. It is in a read-only database.

/query.asp, line 80



What exactly does this mean? I was reading somewhere and I beleive there has to be a setting changed on the server side?

Thanks
Wayne

whammy
09-30-2002, 02:24 AM
What did you type in to get that error? I use the exact same script with no problems (actually using brinkster.com it makes it a lot easier to manage my databases!) - also, what host are you using?

Make sure that the .mdb file is in a folder which you have write permissions to (i.e. if you're using brinkster.com, you'd need to put the file in the /database directory).

If you're not sure where it should go, your host should have relevant information on their website. :)

P.S. That tutorial won't exactly "teach you ASP" although it will get you a good bit down the road... but it will teach you a lot about SQL statements!

For a good basic understanding of ASP I'd recommend the book "Beginning ASP 3.0" by Wrox.com, and perhaps checking out the tutorials I posted and http://www.devguru.com .

Besides, if you are going to use databases in the future (no matter what scripting language), you have to get familiar with SQL!

awayne
09-30-2002, 02:30 AM
the hosting is through www.idmi.net and I beleive they have to give me write permissions to the database folder that I made. Basicly I have to ask them to give me write permissions on my database folder? Thanks for your help.

Wayne

whammy
09-30-2002, 02:35 AM
Good luck. Let me know what they say. :)