ASP Search from Access DB

admin

Administrator
Staff member
I am new to ASP.
I have build a search form of my friends list using ASP & Ms Access Database.
I want that user enter any input like name OR address , only this will or matching result will disply instead of all enteries.

Here is the code of my both files.

---------------------------------------------------------------------
Code of searching form "search.asp "
---------------------------------------------------------------------
<html>
<head>
</head>
<body>
<form method="GET" action="result.asp">
<p><input type="text" name="search" size="20">
<input type="submit" value="Submit" name="B1">
<input type="reset" value="Reset" name="B2"></p>
</form>
</body>
</html>

---------------------------------------------------------------------
Code of result page "result.asp "
---------------------------------------------------------------------

<HTML>
<BODY>
<%
Set MyConn = Server.CreateObject("ADODB.Connection")
MdbFilePath = Server.MapPath("sample.mdb")
MyConn.Open "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & MdbFilePath & ";"
SQL_query = "SELECT * FROM Friends"
Set RS = MyConn.Execute(SQL_query)
WHILE NOT RS.EOF
%>
<LI><%=RS("Name")%> : <%=RS("Address")%>
<%
RS.MoveNext
WEND
%>
</BODY>
</HTML>


I want that user enter any input like name OR address , only this will or matching result will disply instead of all enteries.shehri,

So far you code looks pretty good. All you lacking is a WHERE clause in your SQL statement.

Try something like this:

SQL_query = "SELECT * FROM Friends WHERE Name LIKE '%" & request.querystring("search") & "%' OR Address LIKE '%" & request.querystring("search") & "%'"

Since your form method is GET, you'll have to use request.querystring. If you use form method POST, then the code would change to request.form.

Using the LIKE operator with the '%' sign will give you the broadest result set, so if the user enters 'Bob' as their search, it will match 'Bob', 'Bobby', and 'Jim-Bob'. If you want a precise match, use a '=' sign instead of the LIKE operator, and remove the % signs completely.
 
Back
Top