Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 19th, 2003, 02:26 PM
linh linh is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 245 linh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 m 27 sec
Reputation Power: 6
Get rid of non-numeric character in a text box

Visual Basic 5.0

I had a text box named Subnet. When a user put in a character, a message pop up saying "only number is allowed."
This works. The problem is, the non-numeric character that
was typed into the text box does not go away.

The statement KeyAscii = 0, suppose to work, but it does not.
So then I comment that out and use the code
SendKeys BACKSPACE, True, but it still does not work.

The entire code below works with Access 97 and 2000 when I used KeyAscii = 0. How do I get it to work in VB5 ?

============================================
In Form
Text box named "Subnet"

Code:
Private Sub Subnet_KeyPress(Index As Integer, KeyAscii As Integer)
  
  Public_Numeric_Only (KeyAscii)

End Sub

============================================
in Module1

Public Sub Public_Numeric_Only(KeyAscii As Integer)
    If KeyAscii <> 8 And KeyAscii <> 9 Then 'Allow BACKSPACE, TAB and Period (decimal) through
        'Only digits are valid characters.
        If Chr(KeyAscii) < "0" Or Chr(KeyAscii) > "9" Then
            'KeyAscii = 0 'Set character to null if out of range
            SendKeys BACKSPACE, True
            Beep
            MsgBox "Only Numeric characters (0 to 9) is allowed", vbExclamation, "Error. Numeric characters Only."
        End If
    End If
End Sub

Last edited by linh : November 19th, 2003 at 02:32 PM.

Reply With Quote
  #2  
Old November 19th, 2003, 02:54 PM
Fisherman's Avatar
Fisherman Fisherman is offline
Inherits Programmer.Slacker
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Aug 2003
Location: Between my Id and your Ego
Posts: 2,178 Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 9 h 56 m 45 sec
Reputation Power: 111
Send a message via ICQ to Fisherman Send a message via AIM to Fisherman
My suggestion would be to use the Text_Changed Event of the textbox, and insert the code to check each character as they type it. That way, as soon as the user types in an alpha character in the numeric field, you could immediately display the error, and remove that single character.
__________________
Fisherman

"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction." - A.Einstein

Reply With Quote
  #3  
Old November 19th, 2003, 03:43 PM
linh linh is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 245 linh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 m 27 sec
Reputation Power: 6
reply

Thank you Fisherman for your help. I got it figure out. All of the code below works if I used

Call Public_Numeric_Only (KeyAscii)

If I left out the word Call, then it won 't work correctly.


=========================================
Code:
Private Sub Subnet_KeyPress(Index As Integer, KeyAscii As Integer)
  
  Public_Numeric_Only (KeyAscii)

End Sub

============================================
in Module1

Public Sub Public_Numeric_Only(KeyAscii As Integer)
    If KeyAscii <> 8 And KeyAscii <> 9 Then 'Allow BACKSPACE, TAB and Period (decimal) through
        'Only digits are valid characters.
        If Chr(KeyAscii) < "0" Or Chr(KeyAscii) > "9" Then
            KeyAscii = 0 'Set character to null if out of range
            Beep
            MsgBox "Only Numeric characters (0 to 9) is allowed", vbExclamation, "Error. Numeric characters Only."
        End If
    End If
End Sub

Last edited by linh : November 19th, 2003 at 03:46 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming > Get rid of non-numeric character in a text box


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT