Consulting

Results 1 to 13 of 13

Thread: "Enter" textbox upon UF initialize

  1. #1
    VBAX Regular
    Joined
    Dec 2004
    Posts
    92
    Location

    "Enter" textbox upon UF initialize

    I'm (fairly) sure this is easy, I'm just not seeing it.

    Is there a way to have a certain textbox be the "default" textbox in a userform? I.E. - Have the cursor in it, ready to enter data, without clicking the textbox first.

    I've tried to do this with the UF_Initialize code, but nothing I've tried so far has worked.(any textbox property that even remotely seemed like it MIGHT work).

    Any help is appreciated. Thanks in advance

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Private Sub Userform_Initialize()

    Me.TextBox1.Text = "some value"
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Dec 2004
    Posts
    92
    Location
    Hmmm...Tried that (actually tried --Me.Textbox1.Text = ""--) but I still have to manually "click myself into" the textbox before I can enter data.

    After clicking the TB, the cursor is blinking and ready for me to type something...that's what I was hoping to have, but without having to click the TB first. Just be at that point upon UF intialize.

    I do appreciate the help just the same, XLD.

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Sorry, misunderstood

    [vba]

    Private Sub Userform_Initialize()

    Me.TextBox1.SetFocus
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Regular
    Joined
    Dec 2004
    Posts
    92
    Location
    That makes perfect sense . I wish I would've thought to try that before asking a semi-silly question here, lol.

    This WB is at work, so I'll try first thing tomorrow.

    Thanks again xld.

  6. #6
    VBAX Regular
    Joined
    Jan 2007
    Location
    Dallas area
    Posts
    74
    Location
    Is it possible to not only set the focus on a particular text box, but select the text inside it so that the default value can be typed over, if desired?

    Thanks!

  7. #7
    VBAX Regular
    Joined
    Dec 2004
    Posts
    92
    Location
    Assuming the "Setfocus" property does what I was looking for (and I'm sure it does cause XLD is the master, for real ) :

    [vba]Userform_Initialize
    Me.Textbox1.Value = ""
    Me.TextBox1.SetFocus
    [/vba]

    Would do what you want to do, essentially clear the textbox, then enter into it.

    EDIT: Just re-read what you said...the code here will delete the default before the user even sees it. As far as selecting it but leaving it to be viewed.............xld??? We're shining the bat signal.

  8. #8
       Private Sub UserForm_Initialize()
          TextBox1.SetFocus
      End Sub

    Chris
    ------



  9. #9
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Quote Originally Posted by GreenTree
    Is it possible to not only set the focus on a particular text box, but select the text inside it so that the default value can be typed over, if desired?
    Private Sub UserForm_Initialize()
        With Me.TextBox1
            .Value = "default value"
            .SelStart = 0
            .SelLength = Len(.Text)
            .SetFocus
        End With
    End Sub

  10. #10
    VBAX Regular
    Joined
    Dec 2004
    Posts
    92
    Location
    Sorry I didn't get back to ya today, but (as I'm sure you already knew it would) the SetFocus worked perfectly. Thanks all.

    By the way:

    Mikerickson, that's a much better answer to greentree's question than what I put above.

  11. #11
    VBAX Regular
    Joined
    Jan 2007
    Location
    Dallas area
    Posts
    74
    Location
    Mike,

    Thank you for that! One problem I'm having is that this bit of code seems to work some times, but not others. Seems like the pattern is this:

    The first time the form comes up, the value in the ID textbox is selected as I'd hoped for. I can scroll through the ID's (buttons on the form to go up/down) and each time the form is refreshed, the ID is selected.

    Then I close the form (me.hide).

    The NEXT time the form comes up, however, the ID is NOT selected, and scrolling through them doesn't change that behavior. HOWEVER, if I click in the ID textbox & put the cursor there, then click on Next or Previous, then the ID does come up as selected. And comes up that way for each subsequent refresh of the form. But, when I close the form, I'm back where I started: call it up, ID is not selected, and won't be until I click in that textbox. Then it will be each time the form is refreshed, until I close the form.

    Is there something I'm doing wrong?

    Thanks,

    GT

  12. #12
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Greetings GT,

    If you are not holding onto any vals when you hide the userform, you could change:
    [vba]Me.Hide
    'to
    Unload Me[/vba]

    Hope this helps,

    Mark

  13. #13
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    If you want the default to be put in the text box every time that the UF is unhidden, move it to the Activate event.

Posting Permissions

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