Announcement

Collapse
No announcement yet.

Setting focus on a textbox in userform on Excel VBA

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

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

    Code:
    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:
    Code:
    Private Sub optOther_Click()
        Unload Me
        frmOther.Show
    End Sub

  • #2
    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

    New users should read the Forum Rules before posting

    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.

    Comment


    • #3
      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

      Comment


      • #4
        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

        New users should read the Forum Rules before posting

        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.

        Comment


        • #5
          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
          Code:
          frmOther.txtOther.SetFocus
          after
          Code:
          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

          Comment


          • #6
            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.

            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

            Comment

            Working...
            X