Re: Data Validation for TextBoxes

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Unfortunately, that method will not prevent users from pasting in "invalid" characters. Here is some code that I first posted to the compiled VB newsgroups, but have modified for Excel's VBA world... it will restrict the characters allowed in the TextBox (whether typed or pasted) to only the digits 0 thru 6 (the allowable characters are controlled by the list in the 2nd If..Then statement); also note that I have the routine Beep for invalid characters, although the display of a text message is doable if desired...

'************* START OF CODE *************
Dim LastPosition As Long

Private Sub TextBox1_Change()
Static LastText As String
Static SecondTime As Boolean
If Not SecondTime Then
With TextBox1
If .Text Like "*[!0-6]*" Then
Beep
SecondTime = True
.Text = LastText
.SelStart = LastPosition
Else
LastText = .Text
End If
End With
End If
SecondTime = False
End Sub

Private Sub TextBox1_MouseDown(ByVal Button As Integer, _
ByVal Shift As Integer, _
ByVal X As Single, ByVal Y As Single)
With TextBox1
LastPosition = .SelStart
'Place any other MouseDown event code here
End With
End Sub

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
With TextBox1
LastPosition = .SelStart
'Place any other KeyPress checking code here
End With
End Sub
'************* END OF CODE *************

--
Rick (MVP - Excel)


"Chip Pearson" <chip@xxxxxxxxxxxx> wrote in message news:ueovl41okpfa5n0l0akqbrcofv8onkgs9f@xxxxxxxxxx
You can use the code below to restrict text entry in TextBox1 to the
characters "0" to "6". Any other character is rejected and won't be
placed in the text box.

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case Asc("0") To Asc("6")
' OK
Case Else
KeyAscii = 0
Me.Label1.Caption = "Illegal character."
End Select
End Sub

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)


On Sat, 3 Jan 2009 13:58:00 -0800, ryguy7272
<ryguy7272@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

I'm trying to figure out a way to add Data Validation for TextBoxes. I've
used this technique before:
If TextBox1 = "" Then
MsgBox "Please enter a value for 'Name'!!"
Exit Sub
End If

However, I'm not sure how to limit a TextBox to a range of numbers, such as
1-6. How can this be done? Also, how can I do it for many TextBoxes,
without specifically adding code for each?

A for Each.Next should do it, but I may need to add Data Validation for
several TextBoxes, such as 1-20, and then apply a slightly different set of
Data Validation rules for other TextBoxes, such as 1-4 for TextBoxes 21-25.
How would I go about doing that?

Thanks!!
Ryan---

.



Relevant Pages

  • Re: How do I limit TextBox data entry to 2 digits?
    ... that method will not prevent users from pasting in "invalid" characters. ... Static SecondTime As Boolean ... Private Sub TextBox1_MouseDown(ByVal Button As Integer, ... Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) ...
    (microsoft.public.excel.programming)
  • Re: Limit Textbox inputs to Format
    ... Private Sub txtBox_KeyPress(Index As Integer, KeyAscii As Integer) ... > I have a set of textboxes on a form. ... > associated format string for numeric input. ...
    (microsoft.public.vb.general.discussion)
  • Re: eliminating undesired characters during input
    ... tell the user if they've entered the wrong number of characters -- too few is ... You could simply ignore any non-numeric key that is pressed (except ... Private Sub txtNumber_KeyPress ... And KeyAscii vbKeyBack Then KeyAscii = 0 ...
    (microsoft.public.access.formscoding)
  • Re: Multipage Query
    ... Just alter this to address all the textboxes you want included: ... Perhaps calculate a subtotal for each page and then add the subtotals ... Private Sub CommandButton1_Click ... > Private Sub textbox2_keypress(ByVal keyascii As _ ...
    (microsoft.public.excel.programming)
  • Re: textbox validation
    ... Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) ... > others textboxes to do math thing, ... > everything goes wrong when the user capture characters. ...
    (microsoft.public.excel.programming)