Gopal
Gopal

Reputation: 12002

Only allow numeric values in the textbox

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

How can I do this in VB6?

Upvotes: 8

Views: 78961

Answers (13)

EduTronics
EduTronics

Reputation: 1

Just write simple 1 line In text change Text1.text1=val(text1.text)

Upvotes: 0

Juan David Benitez
Juan David Benitez

Reputation: 1


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

Private Sub txtControl_KeyPress(KeyAscii As Integer)
   KeyAscii = RealKeyascii(txtControl, KeyAscii, 256 ^ 8)
End Sub

Upvotes: 0

Mags Iren
Mags Iren

Reputation: 1

TRY THIS CODE: DISABLES LETTER(A-Z,a-z) AND ACCEPTS BACKSPACE, 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
Else
KeyAscii = 0
End If
End Sub

Upvotes: -1

Chhornkimchheng
Chhornkimchheng

Reputation: 307

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

Upvotes: 1

JEREMIAH EasySoft
JEREMIAH EasySoft

Reputation: 1

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

Upvotes: 0

saeid
saeid

Reputation: 1

If Len(Text2.Text) > 0 Then
    If IsNumeric(Text2.Text) = False Then
        Text2.SetFocus
        CreateObject("WScript.Shell").SendKeys "{BACKSPACE}"
    End If
End If

Upvotes: -1

Shamsudheen Abubakker
Shamsudheen Abubakker

Reputation: 21

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.

Upvotes: 2

Koko
Koko

Reputation: 11

The following may be used for whole numbers:

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

Upvotes: 1

Gung Ariana
Gung Ariana

Reputation: 41

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

Upvotes: 4

Thunder
Thunder

Reputation: 11016

In the text box text Change event, check if the entered value is a number. If it's not a number then set the old value back 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

Upvotes: 7

jac
jac

Reputation: 9726

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

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 with the handle of the TextBox.

Private Sub Form_Load()
    Dim lngResult As Long

    lngResult = ForceNumeric(Text1.hwnd)

End Sub

Upvotes: 2

zsalzbank
zsalzbank

Reputation: 9867

Check this out:

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

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

Upvotes: 1

pinichi
pinichi

Reputation: 2205

Right click on control box > component > Control -> Microsoft Masked Edit Control 6.0.
Or with normal textbox:

Private Sub Text1_Validate(Cancel As Boolean)
 Cancel = Not IsNumeric(Text1.Text)

End Sub

Upvotes: 9

Related Questions