?
Solved

Validating Numbers in Text boxes

Posted on 2000-03-02
15
Medium Priority
?
138 Views
Last Modified: 2010-05-02
Does somebody have a sub or function to force user only to enter numbers including decimals, eg 345.567 in a text box?

Thank you
0
Comment
Question by:bemson
[X]
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
  • 5
  • 4
  • 3
  • +3
15 Comments
 
LVL 1

Accepted Solution

by:
karlww earned 300 total points
ID: 2578116
Make your own ActiveX control out of a text box and be done once and for all.  I'll e-mail you some code if you'd like.
0
 
LVL 3

Expert Comment

by:paulstamp
ID: 2578142
Create a project with a text box text1 and paste the following code in :

Private Sub Text1_KeyPress(KeyAscii As Integer)

If KeyAscii <> Asc(".") and KeyAscii <> 8 Then
    If KeyAscii > Asc("9") Or KeyAscii < Asc("0") Then
        KeyAscii = 0
    End If
End If

End Sub

Note - I've used the ASC(".") style functions in for illustrative purposes - you could replace these with the relevant constants ("." = 46, "9" = 57 and 0 = 48), ie :

If KeyAscii <> 46 and KeyAscii <> 8 Then
    If KeyAscii > 57 Or KeyAscii < 48 Then
        KeyAscii = 0
    End If
End If


The Check for KeyAscii allows the backspace key to operate.

0
 
LVL 9

Expert Comment

by:Ruchi
ID: 2578151
Karlww: don't need to make an ACtive X control for that!!

Here's very simple!

Option Explicit

Private Sub Text1_KeyPress(KeyAscii As Integer)
    If ((KeyAscii >= vbKey0) And (KeyAscii <= vbKey9)) Or (KeyAscii = vbKeyBack) Then 'numeric key
        Exit Sub
    ElseIf KeyAscii = 46 Then ' decimal point
        Exit Sub
    Else
        KeyAscii = 0
    End If
End Sub
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 20

Expert Comment

by:hes
ID: 2578152
Or as:

Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii > 47 And KeyAscii < 58 Or KeyAscii = 46 Then
Else
  KeyAscii = 0
End If
End Sub
0
 
LVL 3

Expert Comment

by:paulstamp
ID: 2578161
hes... small point but your solution prevents the user using backspace !
0
 
LVL 3

Expert Comment

by:paulstamp
ID: 2578164
hes... small point but your solution prevents the user using backspace !
0
 

Expert Comment

by:jawmwc
ID: 2578167
In the Textx_KeyPress
 
  "To include decimal point"
     If KeyAscii = 48 then exit sub

  "For Numbers Only"

     If KeyAscii < 48 or KeyAscii > 57 then

    MsgBox "Only Numbers permitted",0

    KeyAscii = 0
    Textx.SetFocus
    Exit Sub
    End If

0
 

Author Comment

by:bemson
ID: 2578204
Code for activex would be appreciated,

between your answer & other comments I have a solution,

Many thnaks
0
 

Author Comment

by:bemson
ID: 2578206
Code for activex would be appreciated,

between your answer & other comments I have a solution,

Many thnaks
0
 

Author Comment

by:bemson
ID: 2578216
Code for activex would be appreciated,

between your answer & other comments I have a solution,

Many thnaks
0
 
LVL 3

Expert Comment

by:paulstamp
ID: 2579180
As Ruchi says... its a bit of an overhead to make an ActiveX control just for that (additional dependencies, problems with compatibility etc etc), but if you really feel the need :

1Create a new ActiveX control project and place a textbox on it.

0
 
LVL 3

Expert Comment

by:paulstamp
ID: 2579187
Sorry.. accidentally sent half cooked comment. Continue from 1 above to :

2) Load the Microsoft ActiveX Control Interface Wizard - find it in AddIn Manager and check the Loaded box

3) Map the properties of the textbox to the properties of your control so that you are effectively delegating the interface of your control to the underlying textbox.

4) Add the code as above (make sure to pick an example that includes support for the backspace)

As mentioned before... its a bit of a sledgehammer to crack a wallnut - I would only recommend this approach if you needa lot of these numeric-only textboxes.
0
 
LVL 1

Expert Comment

by:karlww
ID: 2580228
I mention ActiveX control because you are not trapping the pasting of non text data into the text box (users will try anything).  Also, what about negative numbers?  You need to include the minus sign.

Here's a brief snipit from my own ActiveX control to control pasting.
Const EM_UNDO = &HC7

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, wParam As Any, lParam As Any) As Long

Private Sub txtNumber_Change()
Dim rc As Long
    If Len(txtNumber) > 0 Then
        If txtNumber = "." Or txtNumber = "-" Or txtNumber = "-." Then Exit Sub
        If Not IsNumeric(txtNumber) Then
            rc = SendMessage(txtNumber.hwnd, EM_UNDO, ByVal 0&, ByVal 0&)
        Else
            If txtNumber = vbNullString Then
                mdValue = 0
            Else
                mdValue = CDbl(txtNumber)
            End If
        End If
    End If
End Sub
0
 
LVL 1

Expert Comment

by:karlww
ID: 2580390
I got cut off too above.  The ActiveX advantage is you do this once and you re-use it in all your projects.

HTH
0
 

Author Comment

by:bemson
ID: 2588968
Thanks to all contributors to this topic.

I hope other users have also gained some new skills from this as I have.

0

Featured Post

Industry Leaders: 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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

770 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