Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Numeric + 1 character to textbox in vb6

Posted on 2006-05-22
5
Medium Priority
?
431 Views
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.
0
Comment
Question by:mindserve
5 Comments
 
LVL 1

Accepted Solution

by:
Brownhead earned 200 total points
ID: 16739296
Here ya go :D

'//Code\\
'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
'\\Code//
0
 
LVL 15

Expert Comment

by:JackOfPH
ID: 16739551
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
0
 
LVL 15

Expert Comment

by:cquinn
ID: 16741033
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)
KeyAscii=ValidateEntry(".1234567890",KeyAscii)
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
Else
    ValidateEntry = 0
End If

ExitHere:
  Exit Function

HandleErr:
  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
0
 
LVL 1

Author Comment

by:mindserve
ID: 16741421
:D Thanks Brownhead!
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 16741434
can your routine validate 6.0.0

Just checking
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) 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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

810 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