?
Solved

keypress event

Posted on 2006-05-07
11
Medium Priority
?
314 Views
Last Modified: 2008-03-10
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
Comment
Question by:caz1805
9 Comments
 
LVL 7

Expert Comment

by:dttri
ID: 16627864
Hello,
Can you give me an example because I don't know this clearly.

Thank you
0
 

Author Comment

by:caz1805
ID: 16627871
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
 
LVL 1

Accepted Solution

by:
Nemesis09 earned 672 total points
ID: 16628131
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 7

Expert Comment

by:dttri
ID: 16628577
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
 
LVL 2

Assisted Solution

by:everlaast
everlaast earned 664 total points
ID: 16628954
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
 

Author Comment

by:caz1805
ID: 16629654
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
 
LVL 7

Expert Comment

by:dttri
ID: 16632618
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
 

Author Comment

by:caz1805
ID: 16638183
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
 
LVL 7

Assisted Solution

by:dttri
dttri earned 664 total points
ID: 16640817
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question