Numeric + 1 character to textbox in vb6

Posted on 2006-05-22
Last Modified: 2010-04-07
Here is the problem.
I need to be able to stop users from entering non numeric data to a textbox but still allow them to enter the decimal sign.
I have tried various routines, but nothing seems to be working.
The user would enter 6.00 as in six dollars so they need the decimal point. I do have a module that strips out unwanted characters, but it does not prevent the user from initially entering the characters. Copy and paste is not an issue.
Any ideas or code would be appreciated on this.
Question by:mindserve
    LVL 1

    Accepted Solution

    Here ya go :D

    'This code requires a textbox named Text1
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    If (Chr(KeyAscii) <> "." And KeyAscii <> vbKeyBack And Not IsNumeric(Chr(KeyAscii))) Then KeyAscii = 0
    End Sub
    LVL 15

    Expert Comment

    Private Sub Text1_KeyPress(KeyAscii As Integer)
    Select Case KeyAscii
        Case 8, 48 To 57, 46        'valid
        Case Else
            MsgBox ("Invalid")
    End Select
    end sub
    LVL 15

    Expert Comment

    Passa list of acceptable keys to the function below, and the ascii code of the key being pressed - it will return the ascii code if the key is in the list, or 0 if not
    Call it from the On_keypress event of a text box eg the following example will restrict key entry to valid numerics plus decimal point

    Private Sub txtNumber_KeyPress(KeyAscii As Integer)
    End Sub

    Public Function ValidateEntry(ValidKeys As String, KeyAscii As Integer) As Integer

    On Error GoTo HandleErr

    If InStr(ValidKeys, Chr$(KeyAscii)) > 0 Or KeyAscii = 8 Then  'KeyAscii=8 allows the backspace
        ValidateEntry = KeyAscii
        ValidateEntry = 0
    End If

      Exit Function

      Select Case Err.Number
        Case Else
           MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "modFunctions.ValidateEntry"
      End Select
      Resume ExitHere
    ' End Error handling block.
    End Function
    LVL 1

    Author Comment

    :D Thanks Brownhead!
    LVL 26

    Expert Comment

    can your routine validate 6.0.0

    Just checking

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Introduction In a recent article ( for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
    Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now