excel vba - Restricting User Input to Integers on a Userform Textbox -


i have textbox on userform trying restrict user input allow integer values. able this, behavior little weird. first, here code:

private sub txtanswer_keypress(byval keyascii msforms.returninteger)    if (keyascii >= 48) , (keyascii <= 57)        me.txtanswer.setfocus    else        keyascii = 0        me.txtanswer.setfocus    end if  end sub 

the problem after user has entered value, focus seems go away textbox. further, if user enter integer value, value deleted textbox (i.e. input gets "eaten"). setfocus lines attempt make control behave correctly, seem have no effect.

all want make sure user doesn't enter "r" (or other non-integer value) in textbox. integer value >= 0 acceptable (including multiple digit values 10 or 1000000).

can see why approach isn't working? i've tried few different approaches , have searched around quite bit, can't find works.

thank you

taking 1 step ahead!

'~~> disable pasting ctrl v , shift + insert private sub textbox1_keydown(byval keycode msforms.returninteger, byval shift integer)     if (shift = 2 , keycode = vbkeyv) or (shift = 1 , keycode = vbkeyinsert)         keycode = 0     end if end sub  '~~> preventing input of non numerics private sub textbox1_keypress(byval keyascii msforms.returninteger)     select case keyascii       case vbkey0 vbkey9, vbkeyback, vbkeyclear, vbkeyleft, _       vbkeyright, vbkeyup, vbkeydown, vbkeytab       case else         keyascii = 0         beep     end select end sub 

Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -