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.

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.

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
commented: thumbs up! +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:

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

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.