View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Shawn[_9_] Shawn[_9_] is offline
external usenet poster
 
Posts: 7
Default Class module to filter textbox entry

To all

I have a module I am trying to use to filter the data entry of all textbox's
so as only to allow integers. I can do it by entering all the textbox
numbers by hand etc.. but was hoping to filter based on the class module.

Below is the code but am stuck as to how to pass the text box name to the
module

AHGA!!

Shawn

' class module called CTBX

Public WithEvents TextCtrl As MSForms.TextBox

Private Sub TextCtrl_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

Call UserForm4.CheckKeyPress(TextCtrl.Name, KeyAscii, True, False)

End Sub

' userform initialization on my form!

Private Sub UserForm_Initialize()
Dim Ctrl As MSForms.Control
Set Coll = New Collection
For Each Ctrl In Me.Controls
If TypeOf Ctrl Is MSForms.TextBox Then
Set TBX = New ctbx
Set TBX.TextCtrl = Ctrl
Coll.Add TBX
End If
Next Ctrl
End Sub

PUBLIC SUB TO FILTER THE INPUT

Public Sub CheckKeyPress(tb As MSForms.TextBox, _
ByVal KeyAscii As MSForms.ReturnInteger, _
Optional ignoreDecimal As Boolean, _
Optional allowNegative As Boolean)

Dim key As String
Dim pos As Integer
Dim selLength As Integer
If KeyAscii < 127 Then

key = Chr(KeyAscii)

' If it is not a number (0 thru 9)...

If Not IsNumeric(key) Then

pos = tb.SelStart
selLength = tb.selLength

If (key = decimalSep And Not ignoreDecimal) _
Or (key = "-" And allowNegative _
And pos = 0) Then
If Not ((InStr(tb, key) = 0) Or (selLength 0 _
And InStr(tb.SelText, key) 0)) Then
KeyAscii = 0
End If
Else
KeyAscii = 0
End If
End If
End If
End Sub

NOTE: CODE WAS PIECED TOGETHER FROM NUMBEROUS SOURCES ONLINE