Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Display only numerics in a text box

Posted on 2000-05-21
10
207 Views
Last Modified: 2010-05-02
I have built a calculator and on the calculate event it verifies that the amount is numeric.  Is there an event that I can have in the change event though that will keep on numbers in the field?

I know there has to be an easy answer for this.
0
Comment
Question by:sluggo050499
  • 4
  • 3
  • 3
10 Comments
 
LVL 9

Expert Comment

by:GivenRandy
ID: 2831237
You can use the IsNumeric() call on the Changed event of the object in question.
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 2831245
You can screen at the KeyPress event to keep user from entering non-numeric keys:


Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii <> vbKeyBack And KeyAscii <> vbKeyReturn Then
    If KeyAscii < 48 Or KeyAscii > 57 Then
         KeyAscii = 0
    End If
End If
End Sub




0
 
LVL 12

Accepted Solution

by:
mark2150 earned 10 total points
ID: 2831269
PaulHews,

I prefer:

IF (INSTR("0123456789." & vbCR & vbBack, CHR(keyascii)) < 1) then  keyascii = 0

Simpler logic and easier to allow addition of special characters. Embedded Numeric constants (48 & 57) are a no-no.

:-)

M
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 38

Expert Comment

by:PaulHews
ID: 2831294
Yeah, you're right, much more elegant, easier to read and easier to add to.  Use the like operator and its even faster:

Private Sub Text1_KeyPress(KeyAscii As Integer)
    If Not (("0123456789." & vbCr & vbBack) Like "*" & Chr(KeyAscii) & "*") Then
        KeyAscii = 0
    End If


End Sub

And also

0
 
LVL 9

Expert Comment

by:GivenRandy
ID: 2831295
Good tip, Mark2150.  You might want to add the negative sign:

If (InStr("-0123456789." & vbCr & vbBack, Chr(KeyAscii)) < 1) Then KeyAscii = 0

Of course, this will allow a negative sign in several positions.  However, both approaches allow several decimal points.
0
 
LVL 9

Expert Comment

by:GivenRandy
ID: 2831312
PaulHews, I tried the LIKE section, but it didn't work.

If you use Chr$() instead of Chr(), it's about 7% faster.
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 2831801
How didn't it work, you mean it wasn't faster?  I just tested it here, and it works the way it is supposed to.  I haven't tested it for speed, but I remember reading in VBPJ that the Like operator performs the comparison much faster than Instr (not that speed is really much of an issue here.  Maybe if you had a large comparison string or pattern.)
0
 
LVL 12

Expert Comment

by:mark2150
ID: 2831822
For keystroke validation I don't think it makes squat of difference.

As for multiple . and -'s, yes, but if you pull a VAL() operator on the text box contents you'll get as much legitimate number as there is. If some moron puts in three .'s everything to the right of the 2nd one will be discarded. This is such a low occurence event and since the VAL() operator will get at least *some* semblance of legitimate data I normally don't worry about it. The VAL() keeps the garbage out of the database or any subsequient calculations.

M
0
 
LVL 9

Expert Comment

by:GivenRandy
ID: 2832675
I agree, speed in this type of thing is insignificant.  I was just throwing it in as something to use in the future (e.g., using Chr$() instead of Chr() is usually faster).  The Like approach (see next paragraph) was 3% slower and the Chr$ was 7% faster -- big deal.  Never be noticed with user input.  With high speed communications or processing, 10% differences would be noticed, but not in this situation.

Paul, I copied in your code verbatim and it did not screen the non-numeric entries -- the first time (last night).  I just did a copy-and-paste again, and it worked.  Chalk it up to operator error, I guess.

Mark, good point about Val().  I wouldn't worry about it either.  :)
0
 
LVL 12

Expert Comment

by:mark2150
ID: 2832976
Thanx.

You can guess how I handle calls from users complaining "I put in 123.456.789 and the program only took 123.456"... (big, evil, grin)

M
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

837 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