We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,787 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

How to set the textbox to accept a range of numbers (like 1, 2 and 3 only)?

Hi, I would like to know how to set the textbox to only accept numbers, also that will only accept numbers 1,2 & 3 only.

Thanks in advanced. :icon_mrgreen:

i tried but i really don't know how.

4
Contributors
5
Replies
1 Week
Discussion Span
1 Year Ago
Last Updated
8
Views
ynehs
Newbie Poster
2 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

The best way is the IsNumeric(inputValue) method saying:

If Not IsNumeric(inputValue) Then
    MsgBox "Please enter a number!"
Else
    do whatever
End If

Another way is using an if statement checking for 1, 2 and 3 - not a wide range of numbers to say:

If not (inputValue = 1 or inputValue = 2 or inputValue = 3) Then

     -do whatever

         Else 

         If IsNull(inputValue) Then
             do whatever
         End If

        Else

        MsgBox "Please enter a number"
End If

After the enter button, or mouse has clicked off the text box - whichever way you're doing it.

StephNicolaou
Posting Whiz in Training
204 posts since Nov 2007
Reputation Points: 77
Solved Threads: 18
Skill Endorsements: 0

Just trap the ascii code, So user only can input numbers.
This following code just accept numbers input only :

Private Sub Text1_KeyPress(KeyAscii As Integer)
    Select Case KeyAscii
        Case 48 To 57, 8 '0-9 and backspace
        'Let these key codes pass through
        Case Else
        'All others get trapped
        KeyAscii = 0 ' set ascii 0 to trap others input
    End Select
End Sub
Jx_Man
Senior Poster
3,522 posts since Nov 2007
Reputation Points: 1,468
Solved Threads: 514
Skill Endorsements: 64

Just trap the ascii code, So user only can input numbers.
This following code just accept numbers input only :

Private Sub Text1_KeyPress(KeyAscii As Integer)
    Select Case KeyAscii
        Case 48 To 57, 8 '0-9 and backspace
        'Let these key codes pass through
        Case Else
        'All others get trapped
        KeyAscii = 0 ' set ascii 0 to trap others input
    End Select
End Sub

Thank you so much for helping me. I still a beginner at this, still learning.
Anyways, May I ask, where should I insert the msgbox code so that everytime the user enters letters or other numbers except from 1,2 and 3 of course, a pop-up msg will show up telling that he entered "Invalid Code" ?

Thanks again in advanced! :cool:

ynehs
Newbie Poster
2 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Private Sub Text1_KeyPress(KeyAscii As Integer)
    Select Case KeyAscii
          Case 48 To 57, 8 
        Case Else
          KeyAscii = 0 
          Msgbox "Your Message Here"

    End Select
End Sub
abelingaw
Posting Whiz in Training
227 posts since Jun 2008
Reputation Points: 79
Solved Threads: 31
Skill Endorsements: 2

Anyways, May I ask, where should I insert the msgbox code so that everytime the user enters letters or other numbers except from 1,2 and 3 of course, a pop-up msg will show up telling that he entered "Invalid Code" ?

Put in trapping case :

Private Sub Text1_KeyPress(KeyAscii As Integer)
    Select Case KeyAscii
        Case 48 To 57, 8 '0-9 and backspace
        'Let these key codes pass through
        Case Else
        'All others get trapped
        KeyAscii = 0 ' set ascii 0 to trap others input
        MsgBox "Invalid Code" 'Put your message here
    End Select
End Sub
Jx_Man
Senior Poster
3,522 posts since Nov 2007
Reputation Points: 1,468
Solved Threads: 514
Skill Endorsements: 64

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0783 seconds using 2.69MB