Solved

Validating Numbers in Text boxes

Posted on 2000-03-02
15
133 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

914 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

19 Experts available now in Live!

Get 1:1 Help Now