Finding records in a database

In this example we will show you how to locate record using the Recordset object's Find method .

Code:

This is the code for findarecord1.php

<form action = "findarecord2.php" method = "post">
Type in a URL to search for :<br>
<input type = "text" name="search" size = "60"><br>
<input type = "submit" name="send" value ="search for URL">
</form>

Now create a file called findarecord2.php and enter this

<%
Dim strSearch , strConnect , strFind
'retrieve the search term from th form
strSearch = Request.Form("search")
'our connection
strConnect =" Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:asp\sampledb.mdb;"
'our ADO constants
Const adOpenStatic = 3
Const adLockReadOnly = 1
Const adCmdTable = 2
Dim objRS
'create an instance of recordset object
Set objRS = Server.CreateObject("ADODB.Recordset")
'open our database
objRS.Open "tblsample" , strConnect , adOpenStatic , adLockReadOnly , adCmdTable
'our search term
strFind = "desc = '" & strSearch & "'"
'find the record if any
objRS.Find strFind
'if no records are found
If objRS.EOF Then
Response.Write "Sorry no records match your search"
'if records are found
Else
Response.Write "id : " & objRS("id") & "<br>"
Response.Write "url : " & objRS("url") & "<br>"
Response.Write "desc : " & objRS("desc") & "<br>"
End If
'close and destroy the objects
objRS.Close
Set objRS = Nothing
%>

Notes :

You will have to change the datasource to the exact path to your database in this example "Data Source = D:\examples\asp\example.mdb"

Sponsors