Solved

Validating Numbers in Text boxes

Posted on 2000-03-02
15
132 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
  • 5
  • 4
  • 3
  • +3
15 Comments
 
LVL 1

Accepted Solution

by:
karlww earned 100 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
 
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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

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

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…
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…
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…
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…

708 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

15 Experts available now in Live!

Get 1:1 Help Now