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