4
Contributors
5
Replies
11
Views
5 Years
Discussion Span
Last Post by Jx_Man
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.

Edited by StephNicolaou: n/a

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

Edited by Jx_Man: code added

Votes + Comments
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:

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

Edited by abelingaw: n/a

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

Edited by Jx_Man: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.