Hey, That did the trick !!
Works like a charm.
Exactly what I was looking for.
This is going to become my basic routine for number validation.
--
AH
"Rick Rothstein (MVP - VB)" wrote:
> > Thanks Rick for the comment buttons very helpful.
> > I tried your code in a new userform and it works great.
> > For my purpose I didn't need "+- signs" for interest rate.
> >
> > I also noted that if I entered 99999.99 or 55.55 and then tried to enter
> > another number i got a beep as it should, the cursor moves to the left
> > most
> > of the entered numbers and i am able to enter more numbers.
> > eg.
> > 99999.99 'beep cursor moves to left
> > 666666666699999.99 ' can add more number to the left of original
>
> I remember fixing that bug once a long time ago. Apparently, I grabbed a
> copy of an older version of that routine which was missing an asterisk in
> one of the Like comparisons. The corrected code is below. Sorry about any
> confusion that might have caused you.
>
> If you want to forbid the plus sign from being typed in (I had figured it
> wouldn't matter to any calculations you did down the line if the user
> happened to enter leading plus sign, so I left it in), simply delete the 2
> occurrences of it inside the long If-Then statement with all the Like
> comparisons in the Intrate_Change event procedure (but don't delete anything
> else). By the way, the LastPosition line that you deleted is needed... it
> allows the user to move the cursor and, if he/she hasn't exceeded the limits
> set for the section, continue typing in at the new location. The code below
> should work fine now exactly as is.
>
> Rick
>
>
> Option Explicit
>
> 'For typing floating point numbers in the TextBox
> '=========================================
> ' Set the maximum number of digits before the
> ' decimal point in the MaxWhole constant. Set
> ' the maximum number of digits after the decimal
> ' point in the MaxDecimal constant.
> Dim LastPosition As Long
>
> Private Sub Intrate_Change()
> Static LastText As String
> Static SecondTime As Boolean
> Const MaxDecimal As Integer = 2
> Const MaxWhole As Integer = 5
> With Intrate
> If Not SecondTime Then
> If .Text Like "*[!0-9.+-]*" Or _
> .Text Like "*.*.*" Or _
> .Text Like "*." & String$(1 + MaxDecimal, "#") Or _
> .Text Like "*" & String$(MaxWhole, "#") & "[!.]*" Or _
> .Text Like "?*[+-]*" Then
> Beep
> SecondTime = True
> .Text = LastText
> .SelStart = LastPosition
> Else
> LastText = .Text
> End If
> End If
> End With
> SecondTime = False
> End Sub
>
> Private Sub Intrate_MouseDown(ByVal Button As Integer, _
> ByVal Shift As Integer, _
> ByVal X As Single, _
> ByVal Y As Single)
> With Intrate
> LastPosition = .SelStart
> 'Place any other MouseDown event code here
> End With
> End Sub
>
> Private Sub Intrate_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
> With Intrate
> LastPosition = .SelStart
> 'Place any other KeyPress checking code here
> End With
> End Sub
>
>
|