Allow only numeric values ​​in text box

I want to make a TextBox control that accepts only numeric values.

How to do it in VB6?

+8
source share
12 answers

In the text field event Change event, check to see if the entered value is a number. If it is not a number, then set the old value again.

 Dim textval As String Dim numval As String Private Sub TextBox1_Change() textval = TextBox1.Text If IsNumeric(textval) Then numval = textval Else TextBox1.Text = CStr(numval) End If End Sub 
+6
source

Right-click Control> Component> Control β†’ Microsoft Masked Edit Control 6.0.
Or with a plain text box:

 Private Sub Text1_Validate(Cancel As Boolean) Cancel = Not IsNumeric(Text1.Text) End Sub 
+8
source

I let the API do this for me. I add this function to the .bas module and call it for any edit control that I only need to set to a number.

 Option Explicit Private Const ES_NUMBER = &H2000& Private Const GWL_STYLE = (-16) Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long 'set an editbox to numeric only - return the previous 'style on success or zero on error Public Function ForceNumeric(ByVal EditControlhWnd As Long) As Long Dim lngCurStyle As Long Dim lngReturn As Long lngCurStyle = GetWindowLong(EditControlhWnd, GWL_STYLE) If lngCurStyle <> 0 Then lngReturn = SetWindowLong(EditControlhWnd, GWL_STYLE, lngCurStyle Or ES_NUMBER) End If ForceNumeric = lngReturn End Function 

To use it, call the function using the TextBox descriptor.

 Private Sub Form_Load() Dim lngResult As Long lngResult = ForceNumeric(Text1.hwnd) End Sub 
+3
source

Check this:

http://www.vbforums.com/showthread.php?t=350067

You need to check each keystroke, or you can do one check at the end.

+1
source

I used this code in my project:

 Private Sub txtReceiptID_KeyPress(KeyAscii As Integer) Dim Keychar As String If KeyAscii > 31 Then Keychar = Chr(KeyAscii) If Not IsNumeric(Keychar) Then KeyAscii = 0 End If End If 

End sub

+1
source

I usually use this code:

 Private Sub text1_KeyPress(KeyAscii As Integer) If Not IsNumeric(text1.Text & Chr(KeyAscii)) And Not KeyAscii = 8 Then KeyAscii = 0 End Sub 
0
source

For integers, the following may be used:

 Private Sub text1_KeyPress(KeyAscii As Integer) If Not IsNumeric(text1.Text & Chr(KeyAscii)) And Not KeyAscii = 8 Then KeyAscii = 0 if (KeyAscii>=43) and (KeyAscii<=46) Then KeyAscii = 0 'it ignores '-', '+', '.' and ',' End Sub 
0
source

I usually use this code:

 Private Sub text1_KeyPress(KeyAscii As Integer) If Not IsNumeric(Chr(KeyAscii)) And Not KeyAscii = 8 Then KeyAscii = 0 End If End Sub 

Hope this helps.

0
source

Try this code:

 Private Sub Text1_Change() textval = Text1.Text If IsNumeric(textval) Then numval = textval Else Text1.Text = CStr(numval) End If End Sub 
0
source

Just select the control, keyPress method and IDE to create the next method for you. Then add the following code inside the method

 Private Sub txtControl_KeyPress(KeyAscii As Integer) KeyAscii = RealKeyascii(txtControl, KeyAscii, 256 ^ 8) End Sub 
0
source
 If Len(Text2.Text) > 0 Then If IsNumeric(Text2.Text) = False Then Text2.SetFocus CreateObject("WScript.Shell").SendKeys "{BACKSPACE}" End If End If 
-one
source

TRY THIS CODE: DISABLE THE LETTER (AZ, AZ) AND TAKES THE BACK SPACE, DOTS, COMMAS AND NUMBERS (0-9) =)

Private Sub Text1_KeyPress (KeyAscii As Integer)
If not (KeyAscii> = vbKeyA and KeyAscii <= vbKeyZ), and not (KeyAscii> = 97 and KeyAscii <= 122), then
still
KeyAscii = 0
End if
End sub

-one
source

All Articles