• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 315
  • Last Modified:

keypress event

hi people,

I have a program which looks up a value in a datbase and find the corresponding value and places it into another text box.  It is fired by the press of the spacebar, this works fine but i was wondering if i could make it fire using the full stop, comma and so forth.  i tried adding extra values keyascii part but when the spacebar was pressed after the full stop it changed back.

here is the code

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

    Dim Prts() As String
    Dim tmpTEXT As String

If KeyAscii = 32  Then
    If TextBox1.Text <> "" Then
        tmpTEXT = TextBox1.Text
        Prts = Split(TextBox1.Text, " ")
        For X = 0 To UBound(Prts)
             rs.FindFirst "[SearchText]='" & Prts(X) & "'"
             If Not rs.NoMatch Then
                  tmpTEXT = Replace(tmpTEXT, Prts(X), rs.Fields("ReplacementText"))
             End If
        Next
       TextBox2.Text = tmpTEXT
    End If
    TextBox2.Text = Replace(tmpTEXT, Chr(9), " ")
End If
End Sub
0
caz1805
Asked:
caz1805
3 Solutions
 
dttriCommented:
Hello,
Can you give me an example because I don't know this clearly.

Thank you
0
 
caz1805Author Commented:
well i enter hello in the textbox1 and it checks agianst the database and it replies in textbox2 bonjour.  This is once the space bar has been pressed. I tried using the command, If KeyAscii = 32 or 48 Then.  This changed the text when the full stop was pressed but it changed back once the spacebar was pressed after the full stop.

thank u
0
 
Nemesis09Commented:
You could try:

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

    Dim Prts() As String
    Dim tmpTEXT As String

If KeyAscii = 32  or KeyAscii = 48 Then
    If TextBox1.Text = TextBox2.Text & " " THEN End Sub
    TextBox2.Text = ""
    If TextBox1.Text <> "" Then
        tmpTEXT = TextBox1.Text
        Prts = Split(TextBox1.Text, " ")
        For X = 0 To UBound(Prts)
             rs.FindFirst "[SearchText]='" & Prts(X) & "'"
             If Not rs.NoMatch Then
                  tmpTEXT = Replace(tmpTEXT, Prts(X), rs.Fields("ReplacementText"))
             End If
        Next
       TextBox2.Text = tmpTEXT
    End If
    TextBox2.Text = Replace(tmpTEXT, Chr(9), " ")
End If
End Sub

I'm still not quite sure what the program does exactly so I hope this helps. It sounds like your trying to translate a block of text, or a sentence or paragraph word by word, with the actuall translation occuring for the whole paragraph (word by word) each time you press "." "," or "space". Is this right?
If so you could try only translating each new word as it is typed and adding this to the end of TextBox2.Text
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
dttriCommented:
Hi,
I think you store your database like this:
'hello'->'bonjour'
'xxx'->'yyy', etc...
so when you use space bar, it split the text using the space as a delimiter <<your code>>Prts = Split(TextBox1.Text, " ")<<end your code>> and it is OK. But when you use '.' the word you get become 'hello.' (notice the '.' after the word hello) and it can't find this word in the database so it return "not match". To solve this problem, just remove the '.' (or any special characters) like this:
tmpText=Replace(tmpText,".","")
tmpText=Replace(tmpText,",","") etc...
then do the compare as usual.
0
 
everlaastCommented:
Try using KeyDown or KeyUp events instead on key pressed. then you dont have to use ascii codes but instead can use
vb key enum (KeyCodeConstants).

Or just use Change event and look at the text in the textbox to see if it is what you need.
0
 
caz1805Author Commented:
Thanks a lot guys for your input much appreciated.  I will try and put all your code into practice and see if any of it works.

Nemesis yeh thats what i want to do and i tried before by adding another keyascii like this keyascii 32 or 48 adn thats where i recieved my error.  I will try your code though and hopefully it can help me.

dttri yeh that is exactly what the matter is with this program.  You are saying remove the punctuation so that the space bar works, but this would not aloow for any punctuation in the program which is not what i want.  I hope that is what you mean.

everlaast I will try and see if using the key_up and key_down events will work, but isnt keyascii = 32 the same as vbkeyspace?? so it will be the same?
The reason i did not use the change event is that i think cant remember now that it would try and translate even though the whole word has not been typed.  so it would translate' you' when im typing 'yours', i think thats why.

thanks again
0
 
dttriCommented:
Hello caz1805,
Oh, I forget that you may want to use the punctuation. If so, you can split your text as previous, then before compare
rs.FindFirst "[SearchText]='" & Prts(X) & "'"
check if the last character of Prts(X) is a punctuation or not (I assume that you only type punctuation at the end of a word, not in the middle). If it is, remove it and remember (store in a variable), then compare the new Prts(X) value (without punctuation), then you add your punctuation after the translated word. If itsn't, do your work as usual.
Hope this help.
0
 
caz1805Author Commented:
thanks alot, thats what i want but i am struggling to actually put it into practice as i dont know much about it.

could you help by putting up an example?

caz
0
 
dttriCommented:
Hi,
Please try this and tell me the result:

Dim lastChar as String
If KeyAscii = 32  or KeyAscii = 48 Then
    If TextBox1.Text <> "" Then
        TextBox2.Text = ""       'clear text box 2
        tmpTEXT = TextBox1.Text
        Prts = Split(TextBox1.Text, " ")
        For X = 0 To UBound(Prts)
             lastChar=""        'reset the temporary variable
             lastChar=Right(Prts(X))
             If lastChar="." then
                    Prts(X)=Left(Prts(X),Len(Prts(X)-1)    'remove the last char (.)
             Else
                    lastChar=""   'nothing
             End if
             rs.FindFirst "[SearchText]='" & Prts(X) & "'"
             If Not rs.NoMatch Then
                  tmpTEXT = Replace(tmpTEXT, Prts(X), rs.Fields("ReplacementText"))
             End If
             tmpTEXT=tmpTEXT & lastChar
       TextBox2.Text = TextBox2.Text + tmpTEXT
        Next
    End If
    TextBox2.Text = Replace(tmpTEXT, Chr(9), " ")
End If
End Sub
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now