Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8049595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:49:33+00:00 2026-06-05T06:49:33+00:00

I have a textbox on a userform that I am trying to restrict user

  • 0

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:

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 (i.e. the input gets “eaten”). The SetFocus lines are my attempt to make the control behave correctly, but 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T06:49:34+00:00Added an answer on June 5, 2026 at 6:49 am

    Taking it one step Ahead!

    '~~> Disable Pasting CTRL V , SHIFT + INSERT
    Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
        If (Shift = 2 And KeyCode = vbKeyV) Or (Shift = 1 And KeyCode = vbKeyInsert) Then
            KeyCode = 0
        End If
    End Sub
    
    '~~> Preventing input of non numerics
    Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
        Select Case KeyAscii
          Case vbKey0 To vbKey9, vbKeyBack, vbKeyClear, vbKeyLeft, _
          vbKeyRight, vbKeyUp, vbKeyDown, vbKeyTab
          Case Else
            KeyAscii = 0
            Beep
        End Select
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have TextBox which allow insert only numeric values (filtering), But when I paste
I have a textbox that I would like for only numbers. But if I
Desktop window application. I have textbox in which user give values in numbers. what
I'm writing a WPF calculator app. In this application I have TextBox that get
I have a UserForm with some textbox entry fields on it that are enabled/disabled
I am using C#.Net. I have textbox which allow only number, decimal and percentage(%)
I have a form with a number of text boxes for user input (this
I am creating a user form. In this I have textbox and a lot
I have textbox to enter only alphabet and numerical values. I do not want
I have a textbox where user can input Regex string which shall be then

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.