Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - ASP Search for Accesss DB?
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

ASP Search for Accesss DB?

 Post Reply Post Reply
Author
kennywhite View Drop Down
Groupie
Groupie


Joined: 26 February 2009
Location: Indy
Status: Offline
Points: 106
Post Options Post Options   Thanks (0) Thanks(0)   Quote kennywhite Quote  Post ReplyReply Direct Link To This Post Topic: ASP Search for Accesss DB?
    Posted: 13 November 2009 at 5:00pm
I've searched around and havent found any good database search scripts. I want to be able to search for keywords in any part of the database and then, of course bring back the matching records.
 
Can anyone point me in the right direction or maybe share some good code?
 
Thanks.
Back to Top
kennywhite View Drop Down
Groupie
Groupie


Joined: 26 February 2009
Location: Indy
Status: Offline
Points: 106
Post Options Post Options   Thanks (0) Thanks(0)   Quote kennywhite Quote  Post ReplyReply Direct Link To This Post Posted: 16 November 2009 at 8:53pm
I found this nifty little script that I am trying to work with. The search works great, but it will currently only look in one field at a time. The fields in my database that I want to be able to search are TITLE, DESCRIPTION and KEYWORDS.
 
Looking at the code you can see that you can currently only search the title, how can I change it so that I can search all three fields at the same time?
 

<%
 Option Explicit
 Response.Buffer = True
 Response.Expires = 0
%>
<html>
<head>
 <style>
  body { font-family : Verdana; font-size : 8pt; }
 </style>
</head>
<body>
<!--#include file="editme.asp"-->
<%
' ADO Constants - Don't Change Them
Const adOpenForwardOnly = 0
Const adLockReadOnly = 1
Const adCmdText = &H0001
Const adUseClient = 3
Dim currentPage
 If Len(Request.QueryString("currentPage")) = 0 Then
        currentPage = 1
    Else
        currentPage = CInt(Request.QueryString("currentPage"))
    End If
Dim recordsToShow
 recordsToShow = 10
 
 

 
  ' Keyword/s to search
Dim strKeyword
 strKeyword = split(Trim(Request.QueryString("look_for")), " ")
 
 
 
 

 ' Title keyword/s to search
Dim strTitle
 strTitle = split(Trim(Request.QueryString("look_for")), " ")

 ' Our Connection Object
Dim con
 Set con = CreateObject("ADODB.Connection")
 con.Open strDB
 
 ' Our Recordset Object
Dim rs
 Set rs = CreateObject("ADODB.Recordset")
 rs.CursorLocation = adUseClient
 rs.PageSize = recordsToShow
 rs.CacheSize = recordsToShow
 
 
  
 
 
 
' Searching the records for the keywords entered
 Select Case UBound(strTitle)
  Case 0   rs.Open "select * from T_WEBLOG where title like '%" & strTitle(0) & "%' order by mydate desc", con, adOpenForwardOnly,
adLockReadOnly, adCmdText
  Case 1  rs.Open "select * from T_WEBLOG where title like '%" & strTitle(0) & "%' and title like '%" & strTitle(1) & "%' order by
mydate desc", con
  Case 2  rs.Open "select * from T_WEBLOG where title like '%" & strTitle(0) & "%' and title like '%" & strTitle(1) & "%' and title
like '%" & strTitle(2) & "%' order by mydate desc", con
  Case Else rs.Open "select * from T_WEBLOG where title like '%" & strTitle(0) & "%' and title like '%" & strTitle(1) & "%' and title
like '%" & strTitle(2) & "%' order by mydate desc", con
 
 
 
 
  End Select
 
 
 
 
 
 ' If the returning recordset is not empty
 If Not rs.EOF Then
Dim totalpages
 totalpages = rs.PageCount
 rs.AbsolutePage = currentPage
 
 ' Showing total number of pages found and the current page number
    Response.Write "Displaying Page " & currentPage & " of " & totalPages & "<br>"
    Response.Write "Total Records Found : " & rs.RecordCount & "<br>"
    Response.Write "Search Results for : " & Request.QueryString("look_for")
    Response.Write "<br><br>"
   
 ' Showing relevant records
 Dim rcount, i, x
 For i = 1 To rs.PageSize
   rcount = i
   If currentPage > 1 Then
    For x = 1 To (currentPage - 1)
     rcount = 10 + rcount
    Next
   End If
  If Not rs.EOF Then
  Response.Write rcount & ") <b><a href=""" & rs("url") & """>" & rs("title") & "</a></b><br>" & vbcrlf
  Response.Write "" & rs("description") & "<br>" & vbcrlf
  Response.Write "URL : " & rs("url") & "</b><br>" & vbcrlf
  Response.Write "Last indexed on : " & rs("mydate") & "<br><br>" & vbcrlf
  rs.MoveNext
  End If
 Next
 
    ' Links to move through the records
    If currentPage > 1 Then
        Response.Write "<a href=""" & Request.ServerVariables("SCRIPT_NAME") & "?currentPage=" & currentPage - 1 & "&look_for=" &
Server.URLEncode(Request.QueryString("look_for")) & """>Back</a>"
  Else
        Response.Write "<u style=""color : silver;"">Back</u>"
    End If
 
 Response.Write "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
 
 If CInt(currentPage) <> CInt(totalPages) Then
        Response.Write "<a href=""" & Request.ServerVariables("SCRIPT_NAME") & "?currentPage=" & currentPage + 1 & "&look_for=" &
Server.URLEncode(Request.QueryString("look_for")) & """>Next</a>"
     Else
        Response.Write "<u style=""color : silver;"">Next</u>"
 End If
 
 Else
 Response.Write "<b>Sorry, no matching record was found.</b>" & vbcrlf 
 
 End If
 
 ' Done. Now release Objects
 con.Close
 Set con = Nothing
 Set rs = Nothing
%>
<br><br><br>
<div align="center" style="color:silver;"></div>
</body></html>
 
 
I've already got this in place
 


  ' Keyword/s to search
Dim strKeyword
 strKeyword = split(Trim(Request.QueryString("look_for")), " ")
 
But I'm not sure how I would go about telling it to search the keyword field here:
 
[code]
' Searching the records for the keywords entered
 Select Case UBound(strTitle)
  Case 0   rs.Open "select * from T_WEBLOG where title like '%" & strTitle(0) & "%' order by mydate desc", con, adOpenForwardOnly,
adLockReadOnly, adCmdText
  Case 1  rs.Open "select * from T_WEBLOG where title like '%" & strTitle(0) & "%' and title like '%" & strTitle(1) & "%' order by
mydate desc", con
  Case 2  rs.Open "select * from T_WEBLOG where title like '%" & strTitle(0) & "%' and title like '%" & strTitle(1) & "%' and title
like '%" & strTitle(2) & "%' order by mydate desc", con
  Case Else rs.Open "select * from T_WEBLOG where title like '%" & strTitle(0) & "%' and title like '%" & strTitle(1) & "%' and title
l
Back to Top
kennywhite View Drop Down
Groupie
Groupie


Joined: 26 February 2009
Location: Indy
Status: Offline
Points: 106
Post Options Post Options   Thanks (0) Thanks(0)   Quote kennywhite Quote  Post ReplyReply Direct Link To This Post Posted: 17 November 2009 at 9:08pm
I still haven't been able to figure out how to search multiple fields at once, but I do have another question. This one seems simple, but I  can't figure it out.
 
When the search results come back the page says how many pages of results there are. I would like to be able to display a number for each page so that the user can view the results on say, page 8 without clicking next 7 times.
 
How can this be done?
Back to Top
dpyers View Drop Down
Senior Member
Senior Member


Joined: 12 May 2003
Status: Offline
Points: 3937
Post Options Post Options   Thanks (0) Thanks(0)   Quote dpyers Quote  Post ReplyReply Direct Link To This Post Posted: 17 November 2009 at 10:06pm
Assuming that Title, Description and Keywords are columns within a single table named Mytable and you want to find the word "Red" in any one of them, your sql should look something like this:
SELECT Title, Description, Keywords FROM Mydatabase.Mytable
WHERE CONTAINS((Title, Description, Keywords), 'Red');
That statement will return a record set with all three columns. At least one of the columns will have the word "Red" in it's data. Didn't test it bt it should work for either MSSQL or MySQL.

As far as paginating the results, the basic concept is to write code that looks at the number of records in the recordset and divides it by the number of records you want per page to determine the number of pages you'll need. The links are then custom made with some routine that takes the start record for the page and the number of records to display as parameters - e.g. recpage.asp?start=0&count=10

There's code on the net for doing that type of thing. You might also check the forum code Bruce uses to paginate.


Lead me not into temptation... I know the short cut, follow me.
Back to Top
kennywhite View Drop Down
Groupie
Groupie


Joined: 26 February 2009
Location: Indy
Status: Offline
Points: 106
Post Options Post Options   Thanks (0) Thanks(0)   Quote kennywhite Quote  Post ReplyReply Direct Link To This Post Posted: 23 November 2009 at 5:27pm
I haven't really attempted messing with paginating the results yet, I'm currently working on the more important issue: Searching every column.
 
I did try modifying my sql statement to be more like what you put above, but that did not work for me. Here is what I SQL statement is currently. Notice I have it looking in both b_content and b_requested, but for some reason it only looks in the second column that I listed, which is b_requested. If I search for something that would only exist in b_content, I get no results.
 

 
 ' Searching the records for the keywords entered
 Select Case UBound(strKeyword)
  Case 0   rs.Open "select * from T_WEBLOG where b_content and b_requested  like '%" & strKeyword(0) & "%' order by b_date desc", con,
adOpenForwardOnly, adLockReadOnly, adCmdText
  Case 1  rs.Open "select * from T_WEBLOG where b_content and b_requested like '%" & strKeyword(0) & "%' and b_content and b_requested
like '%" & strKeyword(1) & "%' order by b_date desc", con
  Case 2  rs.Open "select * from T_WEBLOG where b_content and b_requested like '%" & strKeyword(0) & "%' and b_content and b_requested
like '%" & strKeyword(1) & "%' and b_content and b_requested and b_content like '%" & strKeyword(2) & "%' order by b_date desc", con
  Case Else rs.Open "select * from T_WEBLOG where b_content and b_requested like '%" & strKeyword(0) & "%' and b_content and b_requested
like '%" & strKeyword(1) & "%' and b_content and b_requested like '%" & strKeyword(2) & "%' order by b_date desc", con
  End Select
 
 
Thanks!
Back to Top
dpyers View Drop Down
Senior Member
Senior Member


Joined: 12 May 2003
Status: Offline
Points: 3937
Post Options Post Options   Thanks (0) Thanks(0)   Quote dpyers Quote  Post ReplyReply Direct Link To This Post Posted: 23 November 2009 at 9:36pm
What kind of data base are you using - MS Access, MSSQL, MySQL ?

Lead me not into temptation... I know the short cut, follow me.
Back to Top
dpyers View Drop Down
Senior Member
Senior Member


Joined: 12 May 2003
Status: Offline
Points: 3937
Post Options Post Options   Thanks (0) Thanks(0)   Quote dpyers Quote  Post ReplyReply Direct Link To This Post Posted: 23 November 2009 at 10:13pm
Your basic issue here is the scope of your AND operators. Use parentheses to limit the expressions and also applu search values or each field - e.g
(A = x and B =x)
and
(C = y and X = y)

I fiddled with your code but didn't spend a lot of time error checking:


 Select Case UBound(strKeyword)
  Case 0   rs.Open "select * from T_WEBLOG where
b_content  like '%" & strKeyword(0) & "%' and b_requested  like '%" & strKeyword(0) & "%'
order by b_date desc", con, adOpenForwardOnly, adLockReadOnly, adCmdText

  Case 1  rs.Open "select * from T_WEBLOG where
( b_content  like '%" & strKeyword(0) & "%' and b_requested like '%" & strKeyword(0) & "%' )
and
( b_content like '%" & strKeyword(1) & "%' and b_requested
like '%" & strKeyword(1) & "%' )
order by b_date desc", con

 Case 2  rs.Open "select * from T_WEBLOG where
(b_content like '%" & strKeyword(0) & "%' and b_requested like '%" & strKeyword(0) & "%')
and
(b_content like '%" & strKeyword(1) & "%' and b_requested like '%" & strKeyword(1) & "%')
and
(b_content like '%" & strKeyword(2) & "% and b_requested like '%" & strKeyword(2) & "% and b_content like '%" & strKeyword(2) & "%' )
order by b_date desc", con

  Case Else rs.Open "select * from T_WEBLOG where
(b_content like '%" & strKeyword(0) & "%' and b_requested like '%" & strKeyword(0) & "%' )
and
(b_content like '%" & strKeyword(1) & "%' and b_requested like '%" & strKeyword(1) & "%')
and
(b_content like '%" & strKeyword(2) & "%' and b_requested like '%" & strKeyword(2) & "%' )
order by b_date desc", con

  End Select


Lead me not into temptation... I know the short cut, follow me.
Back to Top
kennywhite View Drop Down
Groupie
Groupie


Joined: 26 February 2009
Location: Indy
Status: Offline
Points: 106
Post Options Post Options   Thanks (0) Thanks(0)   Quote kennywhite Quote  Post ReplyReply Direct Link To This Post Posted: 15 December 2009 at 8:34pm
I tried adding that into my code, but it did not work. I received this error message:

Error Type:
Microsoft VBScript compilation (0x800A0409)
Unterminated string constant
/reports/search.asp, line 233, column 45
Case 1 rs.Open "select * from T_WEBLOG where
--------------------------------------------^

I am using an MS access database, by the way.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.