Keypress nest

  • Thread starter Thread starter Garry Jones
  • Start date Start date
G

Garry Jones

I received help here on how to check input in a text box and only allow
certain keys to be pressed.

It works, brilliant, now I want to use this as a seperate proceedure and
call it from several text boxes.

How to I send back the correct value from the check to "keypress"

I have tried this
______________________________________________________________________
Private Sub TextBox1_KeyPress(ByVal keyascii As MSForms.ReturnInteger)
chkinp (keyascii)
End Sub
______________________________________________________________________

Private Sub TextBox2_KeyPress(ByVal keyascii As MSForms.ReturnInteger)
chkinp (keyascii)
End Sub
______________________________________________________________________

Public Function chkinp(keyascii) As MSForms.ReturnInteger
Select Case keyascii
Case 8 To 10, 13, 27, 44 'Control characters
Case 48 To 57 'numbers
Case Else 'Discard anything else
keyascii = 0
End Select
End Function
______________________________________________________________________

But this does not work, I am missing the exact description of how to
send back the value to the keypress check

Many thanks for any help.

Garry Jones
Sweden
 
Untested, but I would suggest:

Private Sub TextBox1_KeyPress(ByVal keyascii As MSForms.ReturnInteger)
KeyAscii = chkinp(keyascii)
End Sub
______________________________________________________________________

Private Sub TextBox2_KeyPress(ByVal keyascii As MSForms.ReturnInteger)
KeyAscii = chkinp(keyascii)
End Sub
______________________________________________________________________

Public Function chkinp(keyascii) As MSForms.ReturnInteger
Select Case keyascii
Case 8 To 10, 13, 27, 44 'Control characters
Case 48 To 57 'numbers
Case Else 'Discard anything else
keyascii = 0
End Select
chkinp = keyascii
End Function
 
Declare the keyascii parameter in chkinp to the same as the keyascii
parameter in the KeyPress Code

so...
Public Sub chkinp(ByVal keyascii As MSForms.ReturnInteger)

ReturnIntereger is actually a class that has a default Integer property
called Value, and since it is an object, the data inside it will get
returned even though you are using ByVal.

Alan
 
Hi again Garry

This is what classes are for.
Inser a new class module (insert menu). Name the module NumTxt in the
property window. Paste this code into the module:

''' class module text:
Public WithEvents TxtBox As MSForms.TextBox

Private Sub TxtBox_KeyPress(ByVal keyascii As MSForms.ReturnInteger)
Select Case keyascii
Case 8 To 10, 13, 27, 44 'Control characters
Case 48 To 57 'numbers
Case Else 'Discard anything else
keyascii = 0
End Select
End Sub

'''end class text

Now in your userform code:

''' Userform code:

Option Explicit

Dim Num1 As New NumTxt
Dim Num2 As New NumTxt

Private Sub UserForm_Initialize()
Set Num1.TxtBox = Me.TextBox1
Set Num2.TxtBox = Me.TextBox2
End Sub

'' end userform code

and that's it .
 
Ok, had a chance to test it.

Change the function to:

Public Function chkinp(KeyAscii) As Long
Select Case KeyAscii
Case 8 To 10, 13, 27, 44 'Control characters
Case 48 To 57 'numbers
Case Else 'Discard anything else
KeyAscii = 0
End Select
chkinp = KeyAscii
End Function

Works fine, but you probably want to use Harald's class approach.
 
Back
Top