Database search doesn't work properly

Results 1 to 3 of 3

Thread: Database search doesn't work properly

  1. #1
    Join Date
    Dec 1969

    Default Database search doesn't work properly

    I modified a database search query from the internet and it works fine except that if we leave the form blank, the search query would display the entire database. If we enter just one letter, it would display all records that contain that letter in the field. what should i modify the code so that it would search for word(s) rather then letter .<BR>Here is part of the code.<BR>thank you for taking time browsing this message and/or helping me out.<BR><BR>For i = 1 to len(SValue)<BR> If mid(SValue, i, 1) = " " Then<BR> CurrentWord = CurrentWord + 1<BR> Else<BR> Word(CurrentWord) = Word(CurrentWord) + mid(SValue, i, 1)<BR> End If<BR>Next<BR><BR>&#039 Now Lets Build The SQL Statement Based On What Search Type (SType)<BR>&#039 Was Selected<BR><BR>&#039 First Part Of SQL<BR>SQL = "SELECT * FROM " & strTableName & " WHERE "<BR><BR>&#039 For Loop To Concatenate SQL String Together<BR> For i = 1 to WordCounter<BR> If SType = "AllWords" Then<BR> If i &#060;> WordCounter Then<BR> SQL1 = SQL1 & strFieldName & " LIKE &#039%" & Word(i) & "%&#039 AND "<BR> ElseIf i = WordCounter Then<BR> SQL1 = SQL1 & strFieldName & " LIKE &#039%" & Word(i) & "%&#039"<BR> End If<BR><BR>

  2. #2
    Chris Willey Guest

    Default RE: Database search doesn't work properly

    I would handle this in 2 ways:<BR><BR>1) On the page where the form is, add the following Javascript code:<BR><BR>&#060;script language="Javascript"><BR>&#060;!--<BR>function checkForm() {<BR>returnValue = false;<BR>if (document.FORMNAME.FIELDNAME.value == "") {<BR>alert("You must fill out the form.");<BR>document.FORMNAME.FIELDNAME.focus();<B R>} else {<BR>returnValue = true;<BR>}<BR>return returnValue;<BR>}<BR>//--><BR>&#060;/script><BR><BR>Then, add an "onclick" event handler for the Submit button like this:<BR><BR>&#060;input type="submit" value="submit" onclick="return checkForm(this)"><BR><BR>That way, if they try to submit the form without filling it out, they&#039ll get a message telling them so.<BR><BR>2) On the form results page, add the following toward the top:<BR><BR>If Request.QueryString("formfield") = "" Then<BR>Response.Redirect("FORM.HTM")<BR>End If<BR><BR>So they&#039ll be bounced back if they didn&#039t fill out the form. This is insurance in case the Javascript on the previous page didn&#039t work (usually because Javascript has been turned off by the client).<BR><BR>Those two things would handle the first case you present. As for the second case, you could modify the line that reads:<BR><BR>SQL1 = SQL1 & strFieldName & " LIKE &#039%" & Word(i) & "%&#039 AND "<BR><BR>and take out the first "%", like so:<BR><BR>SQL1 = SQL1 & strFieldName & " LIKE &#039" & Word(i) & "%&#039 AND "<BR><BR>That would mean the searches would only return results that BEGIN with the word (or letter) entered. This might cause problems for other searches, however. For example, in your current state, if someone typed "dog", results would include "dog tired" and "the lazy dog". With the above change, "the lazy dog" would not appear. This may not be a problem for you, depending on the kind of data you have in your database. Note that if you decide to make the above change, there are two lines (nearly identical) that would both have to be changed the same way.<BR><BR>Does this get at a solution?

  3. #3
    Join Date
    Dec 1969


    I tried the first part but It still give the same probem. It also gives an error message when the input string ends with a space.<BR> <BR>On the second part, I just can&#039t think of a way to add thescript<BR>on my form field, I have an selected option. Should I modify the script so match all my options?? Also, the name of the form is FORM.asp. Should I leave out the extension??<BR>thanks<BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts