1.11M Members

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

 
0
 

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.

 
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.

 
1
 

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

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:

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

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
You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: