ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Help with KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) (https://www.excelbanter.com/excel-programming/389783-help-keypress-byval-keyascii-msforms-returninteger.html)

Ernie

Help with KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
 
Hi everyone,

the code below works sometimes, and other times it doesn't work. I dont know
why, can someone help me figure out why.

Private Sub txtItemFob_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 8 To 10, 13, 27 'Control characters
Case 45, 46 'negative and period
If KeyAscii = 45 Then 'hypen/negative
If Len(Trim(txtItemFob.Text)) 1 Then
Beep
KeyAscii = 0
End If
End If
Case 48 To 57 'numbers
Case Else 'Discard anything else
Beep
KeyAscii = 0
End Select
End Sub


thanks
ernie

Vergel Adriano

Help with KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
 
Ernie,

Give this a try.


Private Sub txtItemFob_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 8 To 10, 13, 27 'Control characters
Case 45 'negative
If Len(Trim(txtItemFob.Text)) = 1 Then
Beep
KeyAscii = 0
End If
Case 46 'period
If InStr(1, txtItemFob.Text, ".") 0 Then
Beep
KeyAscii = 0
End If
Case 48 To 57 'numbers
Case Else 'Discard anything else
Beep
KeyAscii = 0
End Select
End Sub




--
Hope that helps.

Vergel Adriano


"ernie" wrote:

Hi everyone,

the code below works sometimes, and other times it doesn't work. I dont know
why, can someone help me figure out why.

Private Sub txtItemFob_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 8 To 10, 13, 27 'Control characters
Case 45, 46 'negative and period
If KeyAscii = 45 Then 'hypen/negative
If Len(Trim(txtItemFob.Text)) 1 Then
Beep
KeyAscii = 0
End If
End If
Case 48 To 57 'numbers
Case Else 'Discard anything else
Beep
KeyAscii = 0
End Select
End Sub


thanks
ernie



All times are GMT +1. The time now is 05:30 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com