![]() |
|
|
Published: Wednesday, July 21, 1999 By Christopher Miller This article is the first part in a 4 part series. The series will be a complete tutorial of creating your own search engine and web portal called - Hooray! I will be discussing the following techniques:
Lets get started with part 1. You can view a working copy of this article at: http://www.askasp.com/4guys/four_guys/hooray/sort_search.asp. As a search engine, the main feature of Hooray! will be the ability to search through a database of web sites. This in an extremely easy task, and can be accomplished with very little code. While I am at it, we might as well learn how to sort the records as well. Lets start be creating a dummy Access database: Create an Access database called "CompanyX" with a table called "tblEmployees" (this database is just for a very quick example and will not be used in the search engine we are creating).
Next we want to connect to our new database. (For more information on connecting to databases, be sure to read Connecting to a Database.)
Dim DataConnection, cmdDC, RecordSet
'-- Create object and open database
Set DataConnection = Server.CreateObject("ADODB.Connection")
Set cmdDC = Server.CreateObject("ADODB.Command")
'-- default SQL
SQL = "SELECT * FROM tblEmployees"
cmdDC.CommandText = SQL
'-- Cursor Type, Lock Type
'-- ForwardOnly 0 - ReadOnly 1
'-- For this example we will be using a Static cursor and a Read-Only lock type
RecordSet.Open cmdDC, , 0, 1
You will notice that I have used The next step is to create the output table and our form. Lets start with the output table. First we will create our field headers.
If Not RecordSet.BOF Then
'-- Loop through records until we are at the end
Do Until RecordSet.EOF
'-- Display the fields
We are now going to create our sorting buttons. I like to use the A-Z & Z-A format.
If Request.Form("btnSortID") = "Z-A" Then
Lets move on to searching the database. First we will create the form:
Place the search criteria into a variable:
Then we want to go into Access again to grab the SQL. To do this, we want to decide which fields are able to be searched, in this example we will be using all of them. We then want to use use the OR statement in the SQL and place in the SearchText variable (because of the length of the SQL string, I will be using several lines to keep it organized):
In short, when the Search button is clicked, we sort through the database grabbing all of the records that match our criteria and display the results. Note: in this example, if you click a sort button after you have performed a search, it will pull up all of the records rather than the searched records. To sort the searched records, you can use a flag to determine if the table has been searched through yet. If they have, simply place an ORDER BY statement on the end of your SQL. Do not forget to close and release the database:
Set cmdDC = Nothing That is all there is to it!
Enjoy!
Attachments:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
![]() |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||