Re: Required Field for 7 Numeric digits only



I've not tested your code out, but based on prior experience with this concept... using code only in the KeyPress event will not prevent a user from using Copy/Paste to place any text they want (in this case, non-digits) into the TextBox.

Rick


"Malik" <software_guru@xxxxxxxxxxx> wrote in message news:E3ED3A63-C877-42E4-B3DA-D5066C4404F7@xxxxxxxxxxxxxxxx
Ray,

I am not writting in detail this code but this can improve the keypress code:

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii > 48 And KeyAscii < 58 Then
' You can place rest of your code here
Debug.Print KeyAscii
Else
KeyAscii = 0 ' This will cancel invalid key presses
End If
End Sub


--
Malik


"LRay67" wrote:

Malik, after playing with the code you gave me....below is the code that
works with verifying that it has 7 digits and is a numeric filled textbox and
the remainder of the code works.. Thanks for your input.....

Linda

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii = vbKeyTab Then
If TextBox1 = "" Then
MsgBox "Please enter Accounting Unit Code (7 Digits)"
Application.SendKeys ("{BS}")
TextBox1.Activate
Exit Sub
End If
Dim IsValid As Boolean
IsValid = True
' Checking if TextBox value is 7 digits and all the contents are numeric
or not
IsValid = ((Len(TextBox1.Text) = 7) And IsNumeric(TextBox1.Text))
If IsValid = False Then
MsgBox "Accounting Unit Code must contain 7 Digits only" ' , vbCritical
+ vbOKOnly
Application.SendKeys ("{BS}")
Exit Sub
End If
KeyAscii = 0
TextBox3.Activate
End If
End Sub

"Malik" wrote:

> Hi,
>
> Private Sub TextBox1_LostFocus()
> Dim IsValid As Boolean
>
> IsValid = True
> ' Checking if TextBox value is 7 digits and all the contents are > numeric
> or not
> IsValid = ((Len(TextBox1.Text) = 7) And IsNumeric(TextBox1.Text))
> If IsValid = False Then
> MsgBox "Please enter 7 numeric digits only...", vbCritical + > vbOKOnly
> End If
> End Sub
>
>
> You can improve this by adding this routine at any proper place in your > code
> -- > Malik
>
>
> "LRay67" wrote:
>
> > I have a required field that can contain only 7 numeric digits within > > the
> > textbox. Also want to put in a message box to alert them that this > > field
> > must have 7 numeric digits. Any suggestions on how to write the code > > behind
> > this? Thanks
> >
> > Linda

.



Relevant Pages

  • Re: Required Field for 7 Numeric digits only
    ... Copy/Pasting non-digits into the TextBox. ... Private Sub TextBox1_KeyPress ... MsgBox "Please enter Accounting Unit Code (7 Digits)" ... Dim IsValid As Boolean ...
    (microsoft.public.excel.programming)
  • Re: Required Field for 7 Numeric digits only
    ... Copy/Pasting non-digits into the TextBox (and I am pretty sure what they ... Private Sub TextBox1_KeyPress ... MsgBox "Please enter Accounting Unit Code (7 Digits)" ... Dim IsValid As Boolean ...
    (microsoft.public.excel.programming)
  • Re: Required Field for 7 Numeric digits only
    ... If the user attempts to leave the TextBox where there are not 7 digits in it, the user will get a MessageBox and then be returned to the TextBox to correct their entry. ... Static SecondTime As Boolean ... Private Sub TextBox1_MouseDown(ByVal Button As Integer, ...
    (microsoft.public.excel.programming)
  • RE: Required Field for 7 Numeric digits only
    ... MsgBox "Please enter Accounting Unit Code (7 Digits)" ... Dim IsValid As Boolean ... ' Checking if TextBox value is 7 digits and all the contents are numeric ... "Malik" wrote: ...
    (microsoft.public.excel.programming)
  • Re: Required Field for 7 Numeric digits only
    ... current code for the textbox that I need to only allow 7 numeric digits. ... Private Sub TextBox1_KeyPress ... CommandButton to accept the entry (and then proceed with the rest of your ... You could test the TextBox while it it being typed into and only make the ...
    (microsoft.public.excel.programming)

Loading