...

View Full Version : ASP Help (with search function) Please!!



LA2
01-08-2005, 01:18 AM
Here is a part of my code used in a search function:


Function InstrWord(start, Text, search, compareMethod)
Dim index
Dim charcode
InstrWord = 0
index = start - 1
Do
index = Instr(index + 1, Text, search, compareMethod)
If index = 0 then exit function
If index > 1 then
charcode = asc(ucase(mid(Text, index - 1, 1)))
else
charcode = 32
end if
If charcode < 65 or charcode > 90 then
charcode = asc(ucase(mid(Text, index + len(search), 1)) & " ")
If charcode < 65 or charcode > 29 then
InstrWord = index
Exit Function
End If
End If
Loop

End Function


I'm getting an 'Invalid_use_of_Null' error on line 66, which is:


charcode = asc(ucase(mid(Text, index + len(search), 1)) & " ")


Any clue why? This code was working fine some time ago. I can't think of anything I've changed to create this error, but I've obviously done something :)

ghell
01-11-2005, 03:08 PM
you could stop this error with
If NOT IsNull(...) Then
[do stuff]
End If

i expect it is thrown by trying to find the length of a null value or starting the mid at a null value

what exactly is that code supposed to do because i cant think why it would be used :confused:

LA2
01-11-2005, 03:22 PM
Hmm - thanks Ghell... I'm really not sure why most of it's included. I'm a beginner trying to make sense of someone elses work. Care to 'clean it up' a little? Please?!!!

Here's the entire coding... this is the search box on the main page:

<form name="frmJump" method="post" action="csearch.asp">
<input name="keyword" type="text" id="keyword" size="15">
<input name="btnSearch2" type="submit" id="btnSearch2" value="Search"></form>

- certainly don't see anything wrong there. Then here's the entire content of the search results page (minus some html formatting):

<%
Function InstrWord(start, Text, search, compareMethod)
Dim index
Dim charcode
InstrWord = 0
index = start - 1
Do
index = Instr(index + 1, Text, search, compareMethod)
If index = 0 then exit function
If index > 1 then
charcode = asc(ucase(mid(Text, index - 1, 1)))
else
charcode = 32
end if
If charcode < 65 or charcode > 90 then
charcode = asc(ucase(mid(Text, index + len(search), 1)) & " ")
If charcode < 65 or charcode > 90 then
InstrWord = index
Exit Function
End If
End If
Loop

End Function

strSQL = "select * from ctbl ORDER BY ctitle"
strCon = "Provider=Microsoft.JET.OLEDB.4.0; Data Source = " & server.mappath("../**/**.mdb") & ";"
set db = server.createobject("adodb.connection")
set rs = server.createobject("adodb.recordset")
db.open strCon
rs.open strSQL, strCon
%>

<html>
<head>
<title>Search Results</title>
</head>
<body>

<span class="heading">Search results:</font></span><blockquote>
<p>The search term <i><b><%=request.form("keyword")%></b></i> returned:</p>

<%
if request.form("keyword") <> "" then
emptyresults = "yes"
Do while not rs.EOF
if InstrWord(1, rs("ctitle"), request.form("keyword"), vbTextCompare) or InstrWord(1, rs("cdesc"), request.form("keyword"), vbTextCompare) or InstrWord(1, rs("objectives"), request.form("keyword"), vbTextCompare) or InstrWord(1, rs("topics"), request.form("keyword"), vbTextCompare) then
emptyresults = "no"
%>
<p bgcolor="<%=categoryColor%>"><a href="cview.asp?cid=<%=rs("cid")%>"><%=rs("cid")%> &nbsp; <%=rs("ctitle")%></a></p>
<%end if
%>
<%rs.MoveNext
Loop%>
<%if emptyresults = "yes" then%>
<P>no ctbl found - <a href="csearch.asp">search again</a></P>
<%end if
else%>
<P>you did not enter a search keyword - <a href="search.asp">try again</a></P>
<%end if%>
</blockquote>


</body>
</html>

<%
rs.close
db.close
set rs = nothing
set db = nothing
%>

ghell
01-11-2005, 04:29 PM
i think the function looks for a string inside another string, ie
searching for hell in hello, its 4 long so it checks hell and ello for it

as you are running from a database i would just use the like condition of the where clause (http://www.w3schools.com/sql/sql_where.asp)

this could be

SELECT * FROM ctbl WHERE ctitle LIKE '%" & Request.Form("keyword") & "%' OR ..... ORDER BY ctitle

im not really sure about how good this is, i have only used it once and it wouldnt return anyhting if the keyword was under 3 characters long, ie if you searched for "a" in "would you like to play a game?" it would throw an error but "play" worked fine *shrugs*



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum