I want a text box to accept only numbers(0-9) and only x ammount of them like 99999999 then no more are allowed hope this makes sense
later,
Iceman
Printable View
I want a text box to accept only numbers(0-9) and only x ammount of them like 99999999 then no more are allowed hope this makes sense
later,
Iceman
To limit the amount of character that are entered in a textbox, just set the MaxLength property.
As for limiting the type of character put something like this in the KeyPress event of the textbox
------------------Code:If KeyAscii <> vbKey0 Or KeyAscii <> vbKeyNumpad0 Or ... Then
Text1.Text = Mid(Text1.Text, 0, Len(Text1.Text) - 1)
End If
Ryan
There has GOT to be a better way to do that
In the KeyPress event you could put:
And I don't think there is better way to do it. Email me if I'm wrong.Code:Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not IsNumeric(Chr(KeyAscii)) And KeyAscii <> vbKeyBack Then
KeyAscii = 0
End If
End Sub
HTH
------------------
Visual Basic Programmer
------------------
PolComSoft
You will hear a lot about it.
Here's what I use:
This way it's dynamic and you can use it all over by typing one line of code.
The validate string value is to be set to what ever character you wish the user to enter.
Private Sub Text1_KeyPress(KeyAscii As Integer)
KeyAscii = ValiText(KeyAscii, "0123456789", True)
End Sub
Public Function ValiText(KeyIn As Integer, _
ValidateString As String, Editable As Boolean) As Integer
Dim ValidateList As String
Dim KeyOut As Integer
If Editable = True Then
ValidateList = UCase(ValidateString) & Chr(8)
Else
ValidateList = UCase(ValidateString)
End If
If InStr(1, ValidateList, UCase(Chr(KeyIn)), 1) > 0 Then
KeyOut = KeyIn
Else
KeyOut = 0
Beep
End If
ValiText = KeyOut
End Function
------------------
Then as far as the amount of the numbers entered just set the Maxlenth = to the desire number of digits you wish to see.
This is assuming your using a textbox, Richtextbox or any edit box with that property
I know you can specify the maxlength, but can you specify a min length for a text field?
One way to enforce a minimum length would be to use the Validate eventAnd another way to enforce numeric entry in a text boxVB Code:
Private Sub Text1_Validate(Cancel As Boolean) If Len(Text1.Text) < 5 Then MsgBox "Must be at least 5 digits" Cancel = True End If End SubSo to set a textbox to allow numbers only, you would do:VB Code:
Option Explicit Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _ (ByVal hwnd As Long, ByVal nIndex As Long) As Long Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _ (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long Const GWL_STYLE = -16 Const ES_NUMBER As Long = &H2000& Public Sub SetToNumeric(UText As TextBox, Flag As Boolean) ' ' Allow only numeric input in TextBox ' ' Flag = True - allow numeric only ' = False - allow all ' ' Note: This doesn't prevent pasting alpha into the textbox ' Dim curstyle As Long Dim newstyle As Long curstyle = GetWindowLong(UText.hwnd, GWL_STYLE) If Flag Then curstyle = curstyle Or ES_NUMBER Else curstyle = curstyle And (Not ES_NUMBER) End If newstyle = SetWindowLong(UText.hwnd, GWL_STYLE, curstyle) UText.Refresh End Sub
SetToNumeric myTextBox, True
and to change it back to allow anything to be entered:
SetToNumeric myTextBox, False
You can also use Isnumeric() to limit the entry to numeric only
VB Code:
If IsNumeric(Text1.Text) = False Then MsgBox "Type numeric value", , "Result" Text1.SetFocus Exit Sub End If
Hang on, has no one thought of using the masked edit box? It acts just like a text box, and you can mask the input so it acts exactl how you wants it to.
Project > Components > Select "Microsoft Masked Edit Control 6.0" then click ok.
Then set the mask to 999999999