Posted by: ronaldalversado | March 21, 2008

Search and connect through the database (ODBC)

Searching a database is actually a easy one. It just what i done in some tutorials
It just involves a form, and a page that displays the results.

note: I creating my search to a simple form

Steps in searching through out the database
1. First we had to define variables
2. Searxh field
3. A database connection
4. The search and opening the data
5. Display the outputed search

Firstly to process my example, I want show that there is a form, so what i wanna do is to create a form.Below are the exmple code of the form

<table>
<tr>
<form method=”POST” action=”search_results.asp”>
<td>Search: <input type=”text” name=”txtSearch” size=”50″></td>
</tr>
<tr>
<td><input type=”submit” value=”Search”></td>
</tr>
</table>

Then we could proceed to the search

1. Define variables

<%
Dim strInputSearch ‘Variable for the search word
Dim strCon ‘connect to the database
Dim adoCon ‘Database Connection Variable Object
Dim strSQL ‘SQL query for the database
Dim rsSearch ’search recordset storage
2. Search field

This would be variable that has the search word

strInputSearch = Request.Form(”txtSearch”)

This makes it so people cant inject SQL code and/or cause some unwanted errors

strInputSearch = Replace(strInputSearch,”‘”, “””, 1, -1, 1)

3. Database Connection

This is setting-up the connection

Set adoCon = Server.CreateObject(”ADODB.Connection”)

This is that hold the connection string

strCon = “DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=” &Server.MapPath(”database.mdb”)

Open the connection for the database

adoCon.Open strCon

4. Opening the search from the database

Set the database connection

Set rsSearch = Server.CreateObject(”ADODB.Recordset”)

This is the SQL statement for searching.

strSQL = “SELECT tblTable.* FROM tblTable WHERE tblTable.Field LIKE ‘%” & strInputSearch & “%’;”

Opens the database so we can get the results of the search

rsSearch.Open strSQL, adoCon

5. And then we had to display our search by means of this

If there are no matches then continue

If rsSearch.EOF Then

‘Write out the message that there are no matches
Response.Write(”There are no matches with your search”)

‘If there are matches, continue
Else

‘Loop through the database to display all the matches
DO UNTIL rsSearch.EOF

‘Write out the match found. You need to change Field to your database field you are search (its in red)
Response.Write(rsSearch(”Field”) & “<br>”)

‘Move to next line in database
rsSearch.MoveNext
‘Continue looping through database to display all results found
Loop
End If

note: as we open the database we had to corresponding action to do to close the database

‘Reset server objects
Set rsSearch = Nothing
adoCon.Close
Set adoCon = Nothing
%>


Responses

  1. Very well written code given..

    But i have a doubt as its a access inbuilt utility, so its not sure that it can repair the corrupted database, as in the past i also face this problem and all the inbuilt access repair utilities were unable to repair it.

    So i used a access repair utility from here : http://www.repair-access-file.com/access-mdb-recovery.php

    I scan my database and when i found that its my original data but that was in preview mode then i buy the full version and save my database.


Leave a response

Your response:

Categories