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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

KeyPress(KeyAscii...

Status
Not open for further replies.

rickyoswaldiow

Programmer
Jul 16, 2007
127
GB
I am trying to capture a key press inside a text box, if the user hits the enter key after typing somthing in the block of code will execute

Code:
Private Sub txtOldPass_KeyPress(KeyAscii As Integer)
    If (KeyAscii = 13) Then
        'DO STUFF
    End If
    
End Sub

I have had similar code running in the past but I'm not sure what is going wrong here :|
 
The constant you chose (13) is the Pause key. Is that what you are trying to trap?
 
I think you want KeyDown:

Code:
Private Sub txtOldPass_KeyDown(KeyCode As Integer, Shift As Integer)
    If (KeyCode = 13) Then
        'DO STUFF
    End If

End Sub
 
drewpy71, I think you will find that ascii 13 is carriage return.
 
What I'd truly like to accomplish is a control being enabled when a certain string of text is entered into a text box, i.e.

Code:
Private Sub txtOldPass_AfterUpdate()

    If "" & Me.txtOldPass <> "" Then
        If EncryptPass(Me.txtOldPass) <> lngOriginalPassword Then
            'do nothing
        Else
            Me.txtNewPass.Enabled = True
            Me.txtConPass.Enabled = True
        End If
    End If
    
End Sub

This code only kicks in when the focus is taken away from the text box...
 
Try the Change event, it should suit. Use the .Text property for comparison.
 
rickyoswaldiow,
If your going to use On Key Press then you need to make sure that Enter Key Behavior property of the TextBox is set to New Line in Field otherwise the [tt]KeyPress[/tt] event will never fire because the focus is moved to the next control.

You will probably want to use the [tt]KeyDown[/tt] since it occurs before the [tt]KeyPress[/tt] event, just be sure to reset [tt]KeyAscii[/tt] so the new line character is not fed to the control.

For either event [tt]KeyCode[/tt] = [tt]KeyAscii[/tt] = [tt]vbKeyReturn[/tt] = [tt]13[/tt]

Hope this helps,
CMP

[small]For the best results do what I'm thinking, not what I'm saying.[/small]
(GMT-07:00) Mountain Time (US & Canada)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top