Searching a database is actually an 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
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”) & “
”)
‘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






No Comment Received
Leave A Reply