Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Textbox - Change vs. KeyPress Events 1

Status
Not open for further replies.

anet

Programmer
Jul 10, 2001
35
CA
I have some code on the change event of my textboxes, but I only want it to fire if the user does NOT hit either the delete or enter key. I tried moving my code to the KeyPress event and prefacing it with:
Code:
if KeyAscii <> 8 or KeyAscii <> 127 then
 'my code here
end if
However, then my code no longer works properly (even if I comment out the backspace/delete stuff and just copy it exactly from the change event). Is there any way to refer to the backspace and delete keys in the change event? Thanks.
 
Brush up on your logical operations.

if KeyAscii <> 8 or KeyAscii <> 127 then
' does 8 because it is not 127
' does 127 because it is not 8
end if

Change to
if KeyAscii <> 8 And KeyAscii <> 127 then
'your code here
End if

OR

Select Case KeyAscii
Case 8, 127
Case else
' your code here
End Select

OR

if KeyAscii = 8 Or KeyAscii = 127 then exit sub
'your code here Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Private Sub Text1_KeyPress(KeyAscii As Integer)
If (KeyAscii = vbKeyReturn) Or (KeyAscii = vbKeyDelete) Then
Exit Sub
Else
MsgBox &quot;It works&quot;
End If

End Sub

'please reply if it works
 
Oops! I should have picked up on that. Thanks. However, the problem is that this snippet of code:
Code:
strName = txtGroup.Text
If strName <> &quot;&quot; Then
        Set rs = GetArtist(strName, strField)
        If Not rs.EOF Then
            With rs
                .MoveFirst
                If Not IsNull(!GName) Then
                    txtGroup.Text = !GName
                End If
                txtArtistID = !artistID
                txtGroup.SelStart = lngOrigPos
                txtGroup.SelLength = Len(txtGroup.Text)
            End With
        End If
        Set rs = Nothing
    End If

works properly on the Change event, but not on the KeyPress event. Except, of course, that it fires every time the text box changes and I only want it to fire if the user enters text, not if they hit backspace or delete.

What I am attempting to do with this is:
1. the user types the first letter of a name
2. the program goes to the database, executes a &quot;like&quot; query and pulls up the first name that matches.
3. if this is not the name the user wants they type the 2nd letter.
4. back to step 2, etc.

Any suggestions on how I can fix this? Is there a way to refer to the backspace/delete keys on the Change event rather than KeyPress? Or, can you suggest a modification I can make so that it will work on KeyPress?

Thanks for your help.
 
That is because the KeyPress Event is entered before the text has been changed.

maybe
' Form Declarations
Private mintLastKey as Integer

In keyPress
mintLastKey = Keyascii
exit sub

In Change
if mintLastKey = 8 or mintlastkey = 127 then exit sub Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Thanks John!!! That works perfectly.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top