Results 1 to 19 of 19

Thread: deciding on how many words are entered

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2003
    Posts
    29

    deciding on how many words are entered

    Hi

    I was wondering how would can I know how many words are entered in textbox and how to split the text entered into the textbox in order to use them in SQL query as with or, and or like?

  2. #2
    Fanatic Member
    Join Date
    Sep 2000
    Posts
    770
    Code:
    Dim sqlsearch() as string
    
    sqlsearch = Split(text1.text, chr(32), 1)
    will take text in text one and split it into words and put it in the array sqlsearch. Fromt there you can do

    Code:
    wordcount = ubound(sqlsearch) + 1
    to find the word count

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2003
    Posts
    29
    And how can I use it with my following code so that if an option is selected which requires all the words entered in a text box to be joined by and or search it as a phrase or search tem by joining with or. All the options is in the combobox?

    Thanks

    VB Code:
    1. Private Sub cmdSearch1_Click()
    2. Dim sTarget As String
    3. Dim wordcount As Integer
    4. Dim i As Integer
    5. moRS.MoveFirst
    6. sTarget = Replace$(txtSearch1.Text, "*", "%")
    7. sTarget = Replace$(sTarget, "'", "''")
    8. Dim sqlsearch() As String
    9. sqlsearch = Split(sTarget, Chr(32), 1)
    10. wordcount = UBound(sqlsearch) + 1
    11.  
    12.  
    13. 'Decide on right SQL statement
    14. If optKeyword.Value = True Then
    15. moRS.Find "Keywords like '" & sqlsearch & "'"
    16. ElseIf optTitle.Value = True Then
    17. moRS.Find "Article_Name like '" & sqlsearch & "'"
    18. ElseIf optAuthor.Value = True Then
    19. moRS.Find "Author like '" & sqlsearch & "'"
    20. ElseIf optJournaltitle.Value = True Then
    21. moRS.Find "Journal_Name like '" & sqlsearch & "'"
    22. End If
    23.  
    24.  
    25. If moRS.EOF Then
    26. MsgBox "no match found", vbOKOnly, "sorry"
    27. Else
    28. MsgBox "found " & moRS.Fields("Journal_Name").Value
    29. End If
    30. End Sub

  4. #4
    Fanatic Member
    Join Date
    Sep 2000
    Posts
    770
    Edit your original code so that it can generate both types of sql statements. Check if the option is selected with an IF statement. I have code i wrote for this, but, you sould write it. Its good experiance!

  5. #5
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492
    Sounds like you are looking for:




  6. #6

  7. #7
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492
    Originally posted by MartinLiss
    Is that something you wrote?
    Yes why do you ask ?

  8. #8
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492
    Originally posted by MartinLiss
    Is that something you wrote?
    T'was quite painful

  9. #9
    Fanatic Member
    Join Date
    Sep 2000
    Posts
    770
    Where did you get the sweet icons? I suck at graphics.

  10. #10
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492
    Originally posted by nkad
    Where did you get the sweet icons? I suck at graphics.
    If you look real close at the icons you'll see I modified them..look into snagit (www.camtasia.com)...then zoom in...drop pellets of paint, paintbrush works too, adobe is better....always remember not to use solids :-). Majority our from various apps we have here with a touch of changes to fit my needs.

    Jon

  11. #11
    Lively Member
    Join Date
    May 2003
    Location
    Los Angeles
    Posts
    126
    Jhermiz ...
    Nice work

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Nov 2003
    Posts
    29
    So far I have managed to split the entered string into words and I managed to use the options selected in the combobox and optionlist but I still couln't figure out how I can construct the SQL statement that joins all the splitted words by or or and can u pls show me an example SQL statement where it searchs all the words in an array considering the number of words in the array?


    Private Sub cmdSearch1_Click()
    Dim RS As Recordset
    Dim sqlsearch() As String
    Dim retString() As String
    Dim strSplit As String
    Dim i As Long
    Dim SQL As String

    If Len(Trim(txtInput.Text)) <> 0 Then
    lstCities.Clear
    strSplit = txtInput.Text
    retString = Split(strSplit, Chr(32))

    '****
    'For i = LBound(retString) To UBound(retString)

    'If optKeyword.Value = True And cboChoice.Text = "[find any word]" Then

    'SQL = ???

    '***

    Set RS = DB.OpenRecordset(SQL)

  13. #13
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Try this although I haven't tested it. BTW is this DAO?

    VB Code:
    1. Private Sub cmdSearch1_Click()
    2. Dim RS As Recordset
    3. Dim sqlsearch() As String
    4. Dim retString() As String
    5. Dim strSplit As String
    6. Dim i As Long
    7. Dim SQL As String
    8.  
    9. If LenB(Trim(txtInput.Text)) <> 0 Then
    10.     'lstCities.Clear
    11.     SQL = "Select * From MyTable Where "
    12.     retString = Split(txtInput.Text, " ")
    13.     If optKeyword.Value = True And cboChoice.Text = "[find any word]" Then
    14.         For i = 0 To UBound(retString) ' LBound is always 0 from Split
    15.             SQL = SQL & "MyField = " & retString(i) & " or "
    16.         Next
    17.         ' Remove the trailing " or "
    18.         SQL = Left$(SQL, Len(SQL) - 4)
    19.     End If
    20. End If

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Nov 2003
    Posts
    29
    oh thanx a lot that was a great help for me, haven't tried it yet but yes it is DAO application.

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Nov 2003
    Posts
    29
    I have tried it but it gives a syntax error in where clause which I couldn't figure out...any ideas?

    Thanks a lot for your help!

  16. #16

  17. #17

    Thread Starter
    Junior Member
    Join Date
    Nov 2003
    Posts
    29
    Yeah I tried that before adding single quotes but it still gives syntax error for where

  18. #18

  19. #19

    Thread Starter
    Junior Member
    Join Date
    Nov 2003
    Posts
    29
    Thanks a lot for you help Marty

    SQL works fine now the problem was somewhere else in the code that I sorted now, one small change to the code u have given:

    SQL = Left$(SQL, Len(SQL) - 4) minus 4 needs to be replaced by minus 3 as in the minus 4 it gets rid of ' too.

    So thanks a lot for the help

Posting Permissions

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



Click Here to Expand Forum to Full Width