• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 437
  • Last Modified:

Numeric + 1 character to textbox in vb6

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.
1 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
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
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
mindserveAuthor Commented:
:D Thanks Brownhead!
can your routine validate 6.0.0

Just checking
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now