No of visitors who read this post: 829
Category: Visual basic
Type: Question
Author: Johnrick
Your rating: None Average: 5 (10 votes)

I have a textbox that will accept the quantity of products ordered.

It should allow only numbers.


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

# Comment Id: 3245

There are different ways to create validations on textbox. heres a simple one.
Often, to ensure that users enter only numbers in a text field,
you'll want to validate the text as they enter it. The textbox's
Change() event provides the best place to do so. However, simply
using the Is Numeric() function alone won't do the trick. For
instance, suppose you created the following procedure
private sub text1_change()
if not isnumeric(text1.text) then
end if

# Comment Id: 3262

Your code does the job. But  if  the user input is not number, the whole text is erased. what I want is that the textbox will not allow other keys except numbers and still not remove previous numbers inputted by the user.


# Comment Id: 3278

Aside from the is numeric function, you can also use the keypress. follow this code and give it a try

Private Sub Text1_KeyPress(KeyAscii as Integer) 

'Ignore negatives 
If KeyAscii = 45 then goto EndProc 
'If Key pressed is not numeric then make key Null 
If (KeyAscii < 48) or (Keyascii >57) then KeyAscii = 0 
End Sub 

# Comment Id: 3360

That's a nice idea Mr Techgeek.. That textbox will look more professional using your code..

# Comment Id: 3396

Thanks for the knowledge you've shared with me. It can help a lot for my project. thanks again.

# Comment Id: 3409

You're always welcome.. feel free to ask here in techyv if you have problems.

A lot of expert here will help you..