borderAndreaVB free resources for Visual Basic developersborder
AndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2017 Andrea Tincani
:: Prevents and or Allows certain characters and Numeric in text box or any control that support the KeyPress event

Author  

David Costelloe

Language  

VB4, VB5, VB6

Operating Systems  

Windows 95, 98 and NT
Module

Option Explicit

' Add this to any .bas file:

Public Function OnlyNumericTextInput(ByVal strSource As String, _
    Optional strExtra As String = "") As Integer
   
'* Place this Line in the Textbox_KeyPress event

    Dim strTemper As String
    Const strNormNumbers$ = "0123456789"

    If strExtra > "" Then
        strTemper = strNormNumbers$ & strExtra
    Else
        strTemper = strNormNumbers$
    End If
   
'* BackSpace = 8
    If strSource <> 8 Then
        If InStr(strTemper, Chr(strSource)) = 0 Then
            OnlyNumericTextInput = 0
            Exit Function
        End If
    End If
    OnlyNumericTextInput = strSource
End Function

Usage

Option Explicit

'Usage:
Private Sub text1_KeyPress(KeyAscii As Integer)

    If OnlyNumericTextInput(KeyAscii, ".") = 0 Then
        Beep
        KeyAscii = 0
    End If
End Sub

:: Navigation

Home

Beginners Corner

Previous Tip

Next Tip

:: Search this site
Google
borderAndreaVB free resources for Visual Basic developersborder
borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2017 Andrea Tincaniborder