excel vba - How to put focus to textbox as needed -
i have textbox on userform. if user fails enter in textbox, need trap force entry. can enough, after notifying user tht need make entry, want focus return textbox. right now, doesn't that. here code:
private sub txtanswer_keydown(byval keycode msforms.returninteger, byval shift integer) select case keycode case 13: if me.txtanswer.value = "" temp = msgbox("you need enter answer!", vbcritical + vbokonly, "no answer found!") me.txtanswer.setfocus else recordanswer end if end select end sub this code works fine in message box pops if textbox left blank. after clearing message box, if hit enter again, message box reappears, suggesting focus on textbox. however, if try enter character (like number '1' example) nothing appears in textbox.
can suggest how can focus on textbox in way allow user enter data? thank you!
why not using 'ok' button complete action?
you should not bother users messages while typing in form. @ end.
private sub ok_click() '// validate form if txtanswer.text = vbnullstring msgbox "you need enter answer!", vbexclamation, "no answer found!" txtanswer.setfocus exit sub end if '// have reached here form correct carry on recordanswer end sub if want use behaviour asked try this:
private sub txtanswer_keydown(byval keycode msforms.returninteger, byval shift integer) select case keycode case 13: if me.txtanswer.value = "" temp = msgbox("you need enter answer!", vbcritical + vbokonly, "no answer found!") keycode = 0 else recordanswer end if end select end sub the problem in code setting focus enter key firing afterwards. don't need set focus because textbox has focus need cancel enter key.
Comments
Post a Comment