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 8039499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:35:27+00:00 2026-06-05T03:35:27+00:00

I have a textbox on a userform. If the user fails to enter anything

  • 0

I have a textbox on a userform. If the user fails to enter anything in this textbox, I need to trap that to force an entry. I can do this easily enough, but after notifying the user tht they need to make an entry, I want the focus to return to the textbox. Right now, it doesn’t do that. Here is my code:

Private Sub txtAnswer_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

Select Case KeyCode
    Case 13:
        If Me.txtAnswer.Value = "" Then
            temp = MsgBox("You need to enter an answer!", vbCritical + vbOKOnly, "No Answer Found!")
            Me.txtAnswer.SetFocus
        Else
            recordAnswer
        End If
    End Select

End Sub

This code works fine in that the message box pops up if the textbox is left blank. After clearing the message box, if I hit enter immediately again, the message box reappears, suggesting that the focus is on the textbox. However, if I try to enter a character (like the number ‘1’ for example) nothing appears in the textbox.

Can anybody suggest how I can get the focus back on this textbox in a way that will allow the user to enter data? 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-05T03:35:28+00:00Added an answer on June 5, 2026 at 3:35 am

    Why are you not using an ‘ok’ button to complete the action?

    You should not bother users with messages while they are typing in a form. Do it at the end.

    Private Sub OK_Click()
    
        '// Validate form
        If txtAnswer.Text = vbNullString Then
            MsgBox "You need to enter an answer!", vbExclamation, "No Answer Found!"
            txtAnswer.SetFocus
            Exit Sub
        End If
    
        '// You have reached here so form is correct carry on
        recordAnswer
    
    End Sub
    

    If you really want to use the behaviour you asked for then try this:

    Private Sub txtAnswer_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    
        Select Case KeyCode
        Case 13:
            If Me.txtAnswer.Value = "" Then
                temp = MsgBox("You need to enter an answer!", vbCritical + vbOKOnly, "No Answer Found!")              
                KeyCode = 0
            Else
                recordAnswer
            End If
        End Select
    
    End Sub
    

    The problem is that in your code you are setting focus but the enter key is firing afterwards. You don’t need to set focus because the textbox already has the focus you just need to cancel the enter key.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a TextBox that user can enter search-term in it. Its bind to
I have a UserForm with some textbox entry fields on it that are enabled/disabled
I have a textbox on a userform that I am trying to restrict user
Im creating forum like application and I have textbox where users can enter tags
I'm writing a WPF calculator app. In this application I have TextBox that get
Can you tell me when will use CustomValidator. For eg I have Textbox that
I am creating intellisense as Java's intellisense at C#.I have TextBox which user can
I am creating a user form. In this I have textbox and a lot
I have a textbox named date. I need to enter only 8 letters in
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.