+ Reply to Thread
Results 1 to 10 of 10

Setting focus on a textbox in userform on Excel VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    02-09-2013
    Location
    Canada
    MS-Off Ver
    2010
    Posts
    31

    Setting focus on a textbox in userform on Excel VBA

    I have two userforms: frmTradeSickOther and frmOther. When I click "Other" option in frmTradeSickOther, it'll call frmOther. I am trying to get the user to enter some text in a textbox in frmOther and then click on the button "Add Comment". If the user clicks on "Add Comment" button without typing anything in the textbox, a message box will come up telling the user to type something in.

    My Problem: I am trying to get the cursor to automatically go to the top-left corner of the textbox and blink once the user is returned to frmOther after the error message.

    I got the cursor to go to the text box when frmOther is called from frmTradeSickOther by adding the following code (See line 67-71 in the attached file):

    Private Sub optOther_Click()
        Unload Me
        frmOther.Show
        frmOther.txtOther.SetFocus
    End Sub
    Attached Files Attached Files

  2. #2
    Forum Contributor
    Join Date
    12-31-2012
    Location
    Jhang, Pakistan
    MS-Off Ver
    Excel 2010
    Posts
    250

    Re: Setting focus on a textbox in userform on Excel VBA

    Just change your code next to message box

    Me.txtOther.SetFocus
    Me.txtOther.SelStart = 0
    to

    Unload Me
    frmOther.Show
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    02-09-2013
    Location
    Canada
    MS-Off Ver
    2010
    Posts
    31

    Re: Setting focus on a textbox in userform on Excel VBA

    @hafizimran,

    I tried doing what you said, but it did not work. I have attached a screen shot of where the cursor ends up when I click on the Add Comment button without typing anything in the text box, and then clicking okay to the message box.
    Attached Images Attached Images
    Last edited by jeffreybrown; 02-23-2013 at 10:48 PM. Reason: Please do not quote entire posts.

  4. #4
    Registered User
    Join Date
    02-09-2013
    Location
    Canada
    MS-Off Ver
    2010
    Posts
    31

    Re: Setting focus on a textbox in userform on Excel VBA

    I figured out why the cursor was blinking in the middle of txtOther. There was another textbox behind txtOther. For some reason, the cursor was going to that textbox. I deleted that and now the cursor doesn't appear at all after the message box. I've tried @hafizimran's suggestion.

  5. #5
    Valued Forum Contributor
    Join Date
    09-04-2012
    Location
    india
    MS-Off Ver
    Excel 2007
    Posts
    463

    Re: Setting focus on a textbox in userform on Excel VBA

    check the attached file.....


    i hope it is useful for you....


    Regards
    CA Mahaveer Somani
    Attached Files Attached Files

  6. #6
    Valued Forum Contributor
    Join Date
    09-04-2012
    Location
    india
    MS-Off Ver
    Excel 2007
    Posts
    463

    Re: Setting focus on a textbox in userform on Excel VBA

    Shanthan did you check my attached file?

    Regards
    CA Mahaveer Somani

  7. #7
    Registered User
    Join Date
    02-09-2013
    Location
    Canada
    MS-Off Ver
    2010
    Posts
    31

    Re: Setting focus on a textbox in userform on Excel VBA

    Hi Mahaveer,

    Sorry, I thought I replied to you, but I must have forgotten. I tried what you did, but what I attached was only part of my project. I actually need to be able to call frmOther when the user choose the "Other" choice in the userform frmTradeSickOther.

    Here is a summary of my entire project:
    • At my workplace, we currently have our schedule on paper. My boss makes my schedule on excel and then prints out and posts it.
    • The employees make trades with each other all the time. To do this, we put in a request by filling out a form and submitting it to my boss and she makes the change.
    • Employees aren't allowed to make changes, unless if someone calls in sick in the middle of the night or weekends when the bosses aren't around.
    • I give each employee an account within the excel file. When they make changes to any cell, they'll be promted by the userform frmTradeSickOther. Each choices in this userform will call frmTrade, frmSick and frmOther, where the user will fill out the mandatory information. When the user clicks on "Add Comment" button on each userform, it will consolidate the information into a sencence and add it as a comment to the cell that is being changed. The comment will also include the name of the user that made the change, time and date of the
    • Bosses are able to turn off the mandatory comment function, but employees aren't allowed to do so.
    • Employees aren't allowed to print out the schedule as well to save paper and the environment. They can just e-mail the file to themselves and view it at home when needed.

    I am new to VBA. This is actually my first time working on it. Someone from YahooAnswers! had helped me with everything, but now he doesn't have time to help due to his work. So, I have been building on what he did.
    Last edited by Shanthan; 02-25-2013 at 12:41 PM. Reason: Added some additional infomration.

  8. #8
    Valued Forum Contributor
    Join Date
    09-04-2012
    Location
    india
    MS-Off Ver
    Excel 2007
    Posts
    463

    Re: Setting focus on a textbox in userform on Excel VBA

    ok dear carry on...and if you have any further requirement then you can ask....

    If this was helpful to you then hit on the * button for user reputation...

    Regards
    CA Mahaveer Somani

  9. #9
    Registered User
    Join Date
    02-09-2013
    Location
    Canada
    MS-Off Ver
    2010
    Posts
    31

    Re: Setting focus on a textbox in userform on Excel VBA

    I was able to figure out a way around the issue by disabling the textbox, then enabling it, and then setting focus. Here is the code:

        If Me.txtOther = vbNullString Or Me.txtOther = vbNewLine Then
            Me.Hide
            MsgBox "Please enter the necessary information.", vbInformation, "Required Field"
            frmOther.Show
            Me.txtOther.Enabled = False
            Me.txtOther.Enabled = True
            Me.txtOther.SetFocus
            Exit Sub
        End If

  10. #10
    Valued Forum Contributor
    Join Date
    09-04-2012
    Location
    india
    MS-Off Ver
    Excel 2007
    Posts
    463

    Re: Setting focus on a textbox in userform on Excel VBA

    then now what do you want?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1