Loading
Ozgrid Excel Help & Best Practices Forums

Excel Video Tutorials / Excel Dashboards Reports



Results 1 to 6 of 6

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

  1. #1
    Join Date
    21st February 2013
    Posts
    37

    Setting focus on a textbox in userform on Excel VBA

    Hi,

    I have two userforms: frmTradeSickOther and frmOther. When I click on one of the options 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 btnAddComment. If the user hasn't typed anything in the textbox, a message box will come up telling the user to type something in.

    My Problem: When I "setfocus" on the textbox, which is called txtOther, the cursor just blinks in the center, but I cannot actually type in the textbox until I actually click within the textbox. But, when the message box pops up and the user is returned to frmOther to type something in, the cursor then goes to the right place, which is the top-left corner. Does anyone know why this is happening?

    VB:
    Private Sub btnAddComment_Click()    On Error Resume Next 
         
        txtOther.Value = Trim(txtOther.Value) 
        If Me.txtOther = vbNullString Then 
            Me.Hide 
            MsgBox "Please enter the necessary information.", vbInformation, "Required Field" 
            Me.Show 
            Me.txtOther.SetFocus 
            Exit Sub 
        End If 
         
        ... 
         
         
        Unload Me 
    End Sub 
    
    
    Here is how I call frmOther from frmTradeSickOther:
    VB:
    Private Sub optOther_Click() 
        Unload Me 
        frmOther.Show 
    End Sub 
    
    

    Excel Video Tutorials / Excel Dashboards Reports


  2. #2
    Join Date
    26th January 2003
    Location
    Derbyshire,UK
    Posts
    18,294

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

    To best describe or illustrate your problem you would be better off attaching a dummy workbook, the workbook should contain the same structure and some dummy data of the same type as the type you have in your real workbook - so, if a cell contains numbers & letters in this format abc-123 then that should be reflected in the dummy workbook.

    To attach a file to your post, you need to be using the "Reply to Thread', not the "Quick Reply" button.

    On this page, below the message box, you will find a button labelled 'Manage Attachments'.
    Clicking this button will open a new window for uploading attachments.

    You can upload an attachment either from your computer or from another URL by using the appropriate box on this page.
    Alternatively you can click the Attachment Icon to open this page.

    To upload a file from your computer, click the 'Browse' button and locate the file.

    To upload a file from another URL, enter the full URL for the file in the second box on this page.
    Once you have completed one of the boxes, click 'Upload'.

    Once the upload is completed the file name will appear below the input boxes in this window.
    You can then close the window to return to the new post screen.
    Hope that Helps

    Roy

    For free Excel tools & articles visit my web site

    If I have helped you and you feel like putting your hand in your pocket please make a donation to Children in Need

    RoyUK's Web Site

    royUK's Database Form

    Where to paste code from the Forum

    About me.

  3. #3
    Join Date
    21st February 2013
    Posts
    37

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

    I have attached part of what I have been working on. If you run frmTradeSickOther --> click on "Other" option --> it'll open up frmOther.
    Attached Files. REMINDER! OzGrid accepts no responsibility for ANY adverse effects as a result from downloading attached files. ALWAYS run an up-to-date virus scan and disable macros.

    Excel Video Tutorials / Excel Dashboards Reports


  4. #4
    Join Date
    26th January 2003
    Location
    Derbyshire,UK
    Posts
    18,294

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

    I cannot create any such error.

    Click the button when textbox is empty, it re-opens & I can type in the TextBox

    Also type in the box click the button, no problem

    The etxt isn't added to the comment though
    Hope that Helps

    Roy

    For free Excel tools & articles visit my web site

    If I have helped you and you feel like putting your hand in your pocket please make a donation to Children in Need

    RoyUK's Web Site

    royUK's Database Form

    Where to paste code from the Forum

    About me.

  5. #5
    Join Date
    21st February 2013
    Posts
    37

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

    @royUK,

    Thanks for your response.

    I figured out why the cursor isn't blinking in the top left corner of txtOther (the text box in frmOther, where you need to type in the comment) when frmOther is called from frmTradeSickOther. The solution: I needed to have
    VB:
    frmOther.txtOther.SetFocus 
    
    
    after
    VB:
    frmOther.txtOther.SetFocus 
    
    
    See Line67 to 70. Originally, I had those two lines switched.

    My second problem still remains unsolved: The cursor is still not blinking automatically in the txtOther after the error message pops up if the user had clicked on the Add Comment button without entering anything in the textbox.
    Attached Files. REMINDER! OzGrid accepts no responsibility for ANY adverse effects as a result from downloading attached files. ALWAYS run an up-to-date virus scan and disable macros.

    Excel Video Tutorials / Excel Dashboards Reports


  6. #6
    Join Date
    21st February 2013
    Posts
    37

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

    I wasn't given much help on my problem, but I was able to figure a way around it. I still don't know why SetFocus will not work. Anyways, I disabled the textbox and then enabled it, and then set focus on it. This solved my problem.

    VB:
    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 
    
    

    Excel Video Tutorials / Excel Dashboards Reports


Thread Information

Users Browsing this Thread

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

Possible Answers

  1. Setting focus on form in Excel
    By GPSPOW in forum EXCEL HELP
    Replies: 2
    Last Post: May 8th, 2012, 14:57
  2. Shift Focus From TextBox To Userform
    By Madhart in forum EXCEL HELP
    Replies: 5
    Last Post: August 18th, 2009, 08:52
  3. Set Focus To TextBox On UserForm Initialize
    By Mentor in forum EXCEL HELP
    Replies: 2
    Last Post: March 8th, 2008, 03:25
  4. Set Focus of Cursor in TextBox On UserForm Show
    By akphidelt in forum EXCEL HELP
    Replies: 4
    Last Post: May 18th, 2007, 17:01
  5. Set Focus To TextBox on UserForm
    By tsrwebman in forum EXCEL HELP
    Replies: 5
    Last Post: October 16th, 2006, 20:30

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
  •  
porno