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.
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.
Nice question. is right that IsNumeric() is too broad for your case, and ’s ASCII trap still allows 0 and 4-9. Since your valid set is only {1,2,3}, make the textbox accept exactly one character and then block anything that is not 1, 2, or 3. Also guard against paste and programmatic changes so users cannot bypass KeyPress.
At design time, set Text1.MaxLength = 1. Then add a small bit of state and validate both on keypress and on change:
' Form-level variable to remember the last valid value
Private lastOK As String
Private Sub Form_Load()
lastOK = ""
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
' Allow Backspace; otherwise only the characters 1, 2, or 3
If KeyAscii <> 8 And InStr("123", Chr$(KeyAscii)) = 0 Then
KeyAscii = 0
Beep
End If
End Sub
Private Sub Text1_Change()
' This also catches Paste, drag-drop, and code that sets Text
If Len(Text1.Text) = 0 Then
lastOK = ""
ElseIf InStr("123", Text1.Text) = 0 Then
Beep
Text1.Text = lastOK
Text1.SelStart = Len(Text1.Text)
Else
lastOK = Text1.Text
End If
End Sub About the popup: showing a MsgBox on every invalid keystroke (as in ’s example) will frustrate users. A gentler approach is Beep plus a small Label next to the field that says Invalid code. If you really need a popup, show it once in Text1_LostFocus when the user leaves the field and the value is not 1, 2, or 3.
Bonus UX tip: if the only valid choices are 1, 2, or 3, consider three OptionButtons or a small ComboBox. It prevents invalid input entirely and makes the intent obvious.
Jump to Post— Jx_Man 987Just 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 …
Jump to Post— abelingaw 69Private 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
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 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 We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.