Results 1 to 15 of 15

Thread: Find and replace on a KeyPress

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Posts
    88

    Find and replace on a KeyPress

    Hi guys,

    Does anyone know how to make a thread solved as i have quite a few which have finished.

    Well with this question I have a program that replaces a word (hello) with another word (bonjour) on the click of a button but i was just wondering, I know it can be done actually. But how would you make it so after you wrote the word you press space and then it looks the replacement word up in the database. I have tried it, but instead of after every word it looks up after every letter. There is also a button which replaces it, which is the current method of replacing but i would not like to do it via a press of the space bar.

    Heres the code and any help would be appreciated.

    caz

    VB Code:
    1. Private db As Database
    2. Private rs As Recordset
    3.  
    4. Private Sub cmdFind_Click()
    5. If txtSearchText.Text <> "" Then
    6.  
    7. rs.FindFirst "[SearchText] = '" & txtSearchText.Text & "'"
    8.  
    9. If rs.NoMatch Then
    10. 'not found
    11. MsgBox "No word was found that matched that.", vbExclamation
    12. Else
    13. ' return info
    14. Text1.Text = rs.Fields("ReplacementText")
    15. End If
    16. Else
    17. ' you must enter the SearchText
    18. MsgBox "Please enter a Search Word.", vbExclamation, "Error"
    19.  
    20. End If
    21. End Sub
    22.  
    23. Private Sub Form_Load()
    24.  
    25. Set db = OpenDatabase("C:\Documents and Settings\Matthew\Desktop\TEST.mdb")
    26. Set rs = db.OpenRecordset("tblTEST", dbOpenDynaset)
    27.  
    28. End Sub
    29.  
    30. Private Sub txtSearchText_KeyUp(KeyCode As Integer, Shift As Integer)
    31. Dim Prts() As String
    32.  
    33. If KeyCode = vbKeySpace Then
    34. Prts = Split(SearchText.Text, " ")
    35.  
    36. If txtSearchText.Text <> "" Then
    37.  
    38. rs.FindFirst "[SearchText] = '" & txtSearchText.Text & "'"
    39.  
    40. If rs.NoMatch Then
    41. 'not found
    42. MsgBox "No word was found that matched that.", vbExclamation
    43. Else
    44. ' return info
    45. Text1.Text = rs.Fields("ReplacementText")
    46. End If
    47. Else
    48. ' you must enter the SearchText
    49. MsgBox "Please enter a Search Word.", vbExclamation, "Error"
    50. End If
    51. End Sub

  2. #2
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Find and replace on a KeyPress

    1) what are you doing with this?
    Prts = Split(SearchText.Text, " ")

    2) is that code not working? looks like it should
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Posts
    88

    Re: Find and replace on a KeyPress

    Okay if i dont use the keypress command, the button translates fine but now trying to make it so that when i press space it erm.... translates the word just typed.

    so its focusing on this part of the code
    VB Code:
    1. Private Sub txtSearchText_KeyUp(KeyCode As Integer, Shift As Integer)
    2. Dim Prts() As String
    3.  
    4. If KeyCode = vbKeySpace Then
    5. Prts = Split(SearchText.Text, " ")
    6.  
    7. If txtSearchText.Text <> "" Then
    8.  
    9. rs.FindFirst "[SearchText] = '" & txtSearchText.Text & "'"
    10.  
    11. If rs.NoMatch Then
    12. 'not found
    13. MsgBox "No word was found that matched that.", vbExclamation
    14. Else
    15. ' return info
    16. Text1.Text = rs.Fields("ReplacementText")
    17. End If
    18. Else
    19. ' you must enter the SearchText
    20. MsgBox "Please enter a Search Word.", vbExclamation, "Error"
    21. End If
    22. End Sub

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Posts
    88

    Re: Find and replace on a KeyPress

    any help would be really appreciated, thanks

  5. #5
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Find and replace on a KeyPress

    VB Code:
    1. Private Sub txtSearchText_KeyUp(KeyCode As Integer, Shift As Integer)
    2.     Dim Prts() As String
    3.    
    4.     If KeyCode = vbKeySpace Then
    5.        
    6.         If txtSearchText.Text <> "" Then
    7.             Prts = Split(SearchText.Text, " ")
    8.             For x = 0 To UBound(Prts)
    9.                 rs.FindFirst "[SearchText] = '" & Prts(x) & "'"
    10.                 If Not rs.NoMatch Then
    11.                     text1 = Replace(text1, Prts(x), rs.Fields("ReplacementText"))
    12.                 End If
    13.             Next
    14.         End If
    15. End Sub
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Posts
    88

    Re: Find and replace on a KeyPress

    Thanks static, there is not enough 'End If' statements in that piece of code, tried to fiddle round with it but to no avail. And when i enter a End If statement, the program runs untill i type the word and press space and then the 424 error appears saying there is an object required.

    caz

  7. #7
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Find and replace on a KeyPress

    sorry.. working on 4hrs of sleep quite the birthday party I had last night

    try this:
    VB Code:
    1. Private Sub txtSearchText_KeyUp(KeyCode As Integer, Shift As Integer)
    2.     Dim Prts() As String
    3.    
    4.     If KeyCode = vbKeySpace Then
    5.        
    6.         If txtSearchText.Text <> "" Then
    7.             Prts = Split(SearchText.Text, " ")
    8.             For x = 0 To UBound(Prts)
    9.                 rs.FindFirst "[SearchText] = '" & Prts(x) & "'"
    10.                 If Not rs.NoMatch Then
    11.                     txtSearchText = Replace(txtSearchText, Prts(x), rs.Fields("ReplacementText"))
    12.                 End If
    13.             Next
    14.         End If
    15.     End If
    16. End Sub
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Posts
    88

    Re: Find and replace on a KeyPress

    Sorry just as i wrote that i got it going with a bit of renaming fields and so on but loads of thanks, really appreciate it.

    Does anyone know if i could do multiple replacements as in the program say you type hello it puts bonjour in the textbox below and then if you write bye after hello, the bonjour goes back to hello and an au revior is inserted. If that makes sense I can explain more clearly if needed.

    Thanks again.

    caz

  9. #9
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Find and replace on a KeyPress

    but why would u want to to go back? Hello Bye should be Bonjour au revior....?
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Posts
    88

    Re: Find and replace on a KeyPress

    yeh your right, but when i type in say hello it enters bonjour in the other text box which is correct. But when i enter say bye after hello it undoes the past replace so the output is hello au revior.

  11. #11
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Find and replace on a KeyPress

    did u try the newest code I posted?

    It should trans as u type...
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Posts
    88

    Re: Find and replace on a KeyPress

    Yeh that worked great, but i wanted the original text to stay the same in the txtSearchText textbox and the replacement text to be put in the Text1 textbox. Heres how i changed the code, not very much. The bold Text1 is what i changed, now what happens is what i said in my previous post that the previous replacmenet is reversed?!

    VB Code:
    1. rivate Sub txtSearchText_KeyUp(KeyCode As Integer, Shift As Integer)
    2.     Dim Prts() As String
    3.    
    4.     If KeyCode = vbKeySpace Then
    5.        
    6.         If txtSearchText.Text <> "" Then
    7.             Prts = Split(SearchText.Text, " ")
    8.             For x = 0 To UBound(Prts)
    9.                 rs.FindFirst "[SearchText] = '" & Prts(x) & "'"
    10.                 If Not rs.NoMatch Then
    11.                     [B]Text1[/B] = Replace(txtSearchText, Prts(x), rs.Fields("ReplacementText"))
    12.                 End If
    13.             Next
    14.         End If
    15.     End If
    16. End Sub

  13. #13
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Find and replace on a KeyPress

    give this a shot..

    brain is starting to shut down.. lol.. need sleep

    VB Code:
    1. Private Sub txtSearchText_KeyUp(KeyCode As Integer, Shift As Integer)
    2.     Dim Prts() As String
    3.     Dim tmpText As String
    4.     If KeyCode = vbKeySpace Then
    5.        
    6.         If txtSearchText.Text <> "" Then
    7.             tmpText = txtSearchText
    8.             Prts = Split(SearchText.Text, " ")
    9.             For x = 0 To UBound(Prts)
    10.                 rs.FindFirst "[SearchText] = '" & Prts(x) & "'"
    11.                 If Not rs.NoMatch Then
    12.                     tmpText = Replace(tmpText, Prts(x), rs.Fields("ReplacementText"))
    13.                 End If
    14.             Next
    15.             Text1= tmpText
    16.         End If
    17.     End If
    18. End Sub
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Posts
    88

    Re: Find and replace on a KeyPress

    Static your the best!!!

    that worked perferct. Except one thing the output is not replaced by spaces but by tabs, i will attach a screenshot so everyone can see.

    thanks again,

    caz
    Attached Images Attached Images  

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Posts
    88

    Re: Find and replace on a KeyPress

    I just cant find out why, I have been trawling throught the code but to no avial, there is nothing that says why it places a replaced text a tab after the last replaced word.

    COnfusin?!?

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