Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-03-2012, 07:05 PM
joatmon joatmon is offline Restricting User Input on a TextBox (and setting focus) Windows 7 64bit Restricting User Input on a TextBox (and setting focus) Office 2010 64bit
Novice
Restricting User Input on a TextBox (and setting focus)
 
Join Date: May 2012
Posts: 14
joatmon is on a distinguished road
Default Restricting User Input on a TextBox (and setting focus)

I have a textbox on a userform that I am trying to restrict user input to only allow integer values. I am able to do this, but the behavior is a little weird. First, here is my code:



Code:
Private Sub txtAnswer_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    If (KeyAscii >= 48) And (KeyAscii <= 57) Then
        Me.txtAnswer.SetFocus
    Else
        KeyAscii = 0
        Me.txtAnswer.SetFocus
    End If
End Sub
The problem is that after the user has entered a value, the focus seems to go away from the textbox. Further, if the user does enter an integer value, this value is deleted from the textbox. The SetFocus lines are my attempt to make the control behave correctly, bu they seem to have no effect.

All I want to do is make sure that the user doesn't enter something like "r" (or any other non-integer value) in the textbox. Any integer value >= 0 is perfectly acceptable (including multiple digit values like 10 or 1000000).

Can anybody see why my approach isn't working? I've tried a few different approaches and have searched around quite a bit, but I can't find something that works.

Thank you.
Reply With Quote
  #2  
Old 06-05-2012, 03:01 PM
Colin Legg's Avatar
Colin Legg Colin Legg is offline Restricting User Input on a TextBox (and setting focus) Windows 7 32bit Restricting User Input on a TextBox (and setting focus) Office 2010 32bit
Expert
 
Join Date: Jan 2011
Location: UK
Posts: 369
Colin Legg will become famous soon enough
Default

Hi,

Does this example work for you?

http://www.cpearson.com/excel/TextBox.htm
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Restricting User Input on a TextBox (and setting focus) Restricting paragraph styles without restricting character styles Red Pill Word 5 05-25-2012 01:06 PM
Restricting User Input on a TextBox (and setting focus) Display result in textbox based on the input of another textbox scarymovie Word VBA 5 05-16-2012 07:05 PM
Restricting User Input on a TextBox (and setting focus) Setting focus to specific word document from UserForm SaneMan Word VBA 5 04-01-2011 03:11 PM
Look up an array based on user input johnsmb Excel 2 01-07-2011 01:12 PM
restricting template changes ljfergus Word 0 04-13-2010 03:19 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:33 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2021 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft