Forums

ASP

This topic is locked

Problem with search for same word in two fields

Posted 30 Aug 2006 21:28:17
1
has voted
30 Aug 2006 21:28:17 Johan Arvidsson posted:
Hi,

I have made a small search form with dreamweaver and ASP and everything works fine except that the last field (tfSokord) don't work. In the database I want to search for the same word in two different fields (Besk and Titel) at the same time but it don't work. Can someone help? Thanks!

My code doing the search is:
<%
Dim rsKalenderSok__varPlats
rsKalenderSok__varPlats = "%"
If (Request.Form("plats" <> "" Then
rsKalenderSok__varPlats = Request.Form("plats"
End If
%>
<%
Dim rsKalenderSok__varDatum
rsKalenderSok__varDatum = "%"
If (Request.Form("Datum" <> "" Then
rsKalenderSok__varDatum = Request.Form("Datum"
End If
%>
<%
Dim rsKalenderSok__varTyp
rsKalenderSok__varTyp = "%"
If (Request.Form("Typ" <> "" Then
rsKalenderSok__varTyp = Request.Form("Typ"
End If
%>
<%
Dim rsKalenderSok__varOrd
rsKalenderSok__varOrd = "%"
If (Request.Form("tfSokord" <> "" Then
rsKalenderSok__varOrd = Request.Form("tfSokord"
End If
%>
<%
Dim rsKalenderSok
Dim rsKalenderSok_numRows

Set rsKalenderSok = Server.CreateObject("ADODB.Recordset"
rsKalenderSok.ActiveConnection = MM_connKalender_STRING
rsKalenderSok.Source = "SELECT * FROM FromIdag WHERE Plats LIKE '" + Replace(rsKalenderSok__varPlats, "'", "''" + "' AND Datum LIKE '" + Replace(rsKalenderSok__varDatum, "'", "''" + "' AND Kategori LIKE '" + Replace(rsKalenderSok__varTyp, "'", "''" + "' AND besk LIKE '" + Replace(rsKalenderSok__varOrd, "'", "''" + "' AND Titel LIKE '" + Replace(rsKalenderSok__varOrd, "'", "''" + "'"
rsKalenderSok.CursorType = 0
rsKalenderSok.CursorLocation = 2
rsKalenderSok.LockType = 1
rsKalenderSok.Open()

rsKalenderSok_numRows = 0
%>

Replies

Replied 28 Sep 2006 16:59:06
28 Sep 2006 16:59:06 Vince Baker replied:
the way your search query is built means that the word you are searching for must exist in both fields in the same row. To acheive the result where it looks in one or the other you must change your code from and to or, for example:

Set rsKalenderSok = Server.CreateObject("ADODB.Recordset"
rsKalenderSok.ActiveConnection = MM_connKalender_STRING
rsKalenderSok.Source = "SELECT * FROM FromIdag WHERE Plats LIKE '" + Replace(rsKalenderSok__varPlats, "'", "''" + "' AND Datum LIKE '" + Replace(rsKalenderSok__varDatum, "'", "''" + "' AND Kategori LIKE '" + Replace(rsKalenderSok__varTyp, "'", "''" + "' AND (besk LIKE '" + Replace(rsKalenderSok__varOrd, "'", "''" + "' or Titel LIKE '" + Replace(rsKalenderSok__varOrd, "'", "''" + "')"
rsKalenderSok.CursorType = 0
rsKalenderSok.CursorLocation = 2
rsKalenderSok.LockType = 1
rsKalenderSok.Open()

The or must be kept together inside brackets as well to ensure that this is only one decision. (i have added them to your code.) You will see this clearer when you view the code in the recordset advanced mode window.

Regards

Vince Baker
<strong>DMX Zone Manager</strong>

[VBScript | ASP | HTML | CSS | SQL | Oracle | AS400 | ERP Logic | Hosting]

Reply to this topic