SetFocus to first textbox on userform upon Userform1.Show

Discussion in 'Microsoft Excel Programming' started by Guest, Feb 10, 2004.

  1. Guest

    Guest Guest

    I have Userform1 containing several textboxes. When I first open the Excel file and open the Userform for the first time, the cursor correctly appears in the first textbox (Textbox1). However, if I exit the Userform and then go back in, the cursor appears in another Textbox instead of Textbox1. In fact, each time I exit the Userform and go back in, the cursor is in a different Textbox. (The TabIndex of Textbox1 is set to 0.

    I've seen answers to several posts here giving a solution of Userform1.Textbox1.SetFocus. I've put that line in the Private Sub UserForm_Initialize() code, but it has not affect

    What code can I use so that whenever I open (show) Userform1, the cursor is always in Textbox1, and where do I place such code

    Many thanks
    Pau
     
    Guest, Feb 10, 2004
    #1
    1. Advertisements

  2. Guest

    Bob Phillips Guest

    Paul,

    It might be because the userform is being hidden, and so when it is next
    shown, the Initialize event is to used, that is for when it is loaded into
    memory.

    Throw your code into the Userform_Activate() event code, and see if that
    fixes it.

    --

    HTH

    Bob Phillips
    ... looking out across Poole Harbour to the Purbecks
    (remove nothere from the email address if mailing direct)

    "Paul Simon" <> wrote in message
    news:...
    > I have Userform1 containing several textboxes. When I first open the

    Excel file and open the Userform for the first time, the cursor correctly
    appears in the first textbox (Textbox1). However, if I exit the Userform
    and then go back in, the cursor appears in another Textbox instead of
    Textbox1. In fact, each time I exit the Userform and go back in, the cursor
    is in a different Textbox. (The TabIndex of Textbox1 is set to 0.)
    >
    > I've seen answers to several posts here giving a solution of

    Userform1.Textbox1.SetFocus. I've put that line in the Private Sub
    UserForm_Initialize() code, but it has not affect.
    >
    > What code can I use so that whenever I open (show) Userform1, the cursor

    is always in Textbox1, and where do I place such code.
    >
    > Many thanks,
    > Paul
    >
    >
     
    Bob Phillips, Feb 10, 2004
    #2
    1. Advertisements

  3. Guest

    Guest Guest

    Thanks very much Bob. Your first paragraph was the clue to my problem. I changed from Hide to Unload Me, and now it works perfectly. Thanks again, Bob - I appreciate it

    Pau

    ----- Bob Phillips wrote: ----

    Paul

    It might be because the userform is being hidden, and so when it is nex
    shown, the Initialize event is to used, that is for when it is loaded int
    memory

    Throw your code into the Userform_Activate() event code, and see if tha
    fixes it

    --

    HT

    Bob Phillip
    ... looking out across Poole Harbour to the Purbeck
    (remove nothere from the email address if mailing direct

    "Paul Simon" <> wrote in messag
    news:..
    > I have Userform1 containing several textboxes. When I first open th

    Excel file and open the Userform for the first time, the cursor correctl
    appears in the first textbox (Textbox1). However, if I exit the Userfor
    and then go back in, the cursor appears in another Textbox instead o
    Textbox1. In fact, each time I exit the Userform and go back in, the curso
    is in a different Textbox. (The TabIndex of Textbox1 is set to 0.
    >> I've seen answers to several posts here giving a solution o

    Userform1.Textbox1.SetFocus. I've put that line in the Private Su
    UserForm_Initialize() code, but it has not affect
    >> What code can I use so that whenever I open (show) Userform1, the curso

    is always in Textbox1, and where do I place such code
    >> Many thanks

    > Pau
    >>
     
    Guest, Feb 11, 2004
    #3
  4. Guest

    Bob Phillips Guest

    Paul,

    Could I advise you to re-consider. Sometimes, often, it is better to hide a
    sheet rather than unload it, thereby removing that load overhead if you
    re-load in the same session. You can still achieve your objective using the
    Activate event.

    Not saying you should, just make sure you are happy which is the best way
    for you.

    --

    HTH

    Bob Phillips
    ... looking out across Poole Harbour to the Purbecks
    (remove nothere from the email address if mailing direct)

    "Paul Simon" <> wrote in message
    news:...
    > Thanks very much Bob. Your first paragraph was the clue to my problem. I

    changed from Hide to Unload Me, and now it works perfectly. Thanks again,
    Bob - I appreciate it.
    >
    > Paul
    >
     
    Bob Phillips, Feb 11, 2004
    #4
  5. Guest

    Guest Guest

    Hi Bob,

    You are absolutely right! If I load and unload the form several times, I eventually run out of memory. In fact, I even reached a point where Excel told me it could not save the file. (And exiting Excel does not regain the memory. You actually have to reboot.)

    Thanks very much for great advice, Bob.

    Paul




    ----- Bob Phillips wrote: -----

    Paul,

    Could I advise you to re-consider. Sometimes, often, it is better to hide a
    sheet rather than unload it, thereby removing that load overhead if you
    re-load in the same session. You can still achieve your objective using the
    Activate event.

    Not saying you should, just make sure you are happy which is the best way
    for you.

    --

    HTH

    Bob Phillips
    ... looking out across Poole Harbour to the Purbecks
    (remove nothere from the email address if mailing direct)

    "Paul Simon" <> wrote in message
    news:...
    > Thanks very much Bob. Your first paragraph was the clue to my problem. I

    changed from Hide to Unload Me, and now it works perfectly. Thanks again,
    Bob - I appreciate it.
    >> Paul

    >
     
    Guest, Feb 11, 2004
    #5
  6. Guest

    Bob Phillips Guest

    Paul,

    As I think you suspected, it is unloaded at this point.

    Good news though, there is a QueryClose event that is invoked whenever the
    form is unloaded. There are 4 close circumstances, so you can trap them and
    react as you see fit. Here is some code

    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    Select Case CloseMode
    Case vbFormControlMenu:
    Cancel = True 'The user has chosen the Close command from the
    Control menu on the UserForm.
    Case vbFormCode:
    'The Unload statement is invoked from code.
    Case vbAppWindows:
    'The current Windows operating environment
    session is ending.
    Case vbAppTaskManager:
    'The Windows Task Manager is closing the
    application
    End Select

    End Sub

    In this I have shown all 4 instabces, but the only one that is trated is the
    Control Menu close, the X that is, where I cancel the close. So it has no
    effect to click the X.

    You could action some of the others, but be careful, you might never be able
    to close the dang thing.

    --

    HTH

    Bob Phillips
    ... looking out across Poole Harbour to the Purbecks
    (remove nothere from the email address if mailing direct)

    "Paul Simon" <> wrote in message
    news:...
    > Hi Bob,
    >
    > I have a follow-up question. First of all, your suggestion of my putting

    my Userform1.Textbox1.SetFocus line in the Userform_Activate event and
    changing the code in my Exit button back to Userform1.Hide (instead of
    Unload Me) all works perfectly.
    >
    > What I'm wondering is this: If the user clicks the Close button (the

    built-in "x" button at the top right corner of the Userform) instead of my
    Exit button, is the Userform being hidden or unloaded? If unloaded, is
    there a way to remove the "x" Close button from the Userform?
    >
    > Many thanks,
    > Paul
    >
    > ----- Paul Simon wrote: -----
    >
    > Hi Bob,
    >
    > You are absolutely right! If I load and unload the form several

    times, I eventually run out of memory. In fact, I even reached a point
    where Excel told me it could not save the file. (And exiting Excel does not
    regain the memory. You actually have to reboot.)
    >
    > Thanks very much for great advice, Bob.
    >
    > Paul
    >
    >
    >
    >
    > ----- Bob Phillips wrote: -----
    >
    > Paul,
    >
    > Could I advise you to re-consider. Sometimes, often, it is

    better to hide a
    > sheet rather than unload it, thereby removing that load overhead

    if you
    > re-load in the same session. You can still achieve your

    objective using the
    > Activate event.
    >
    > Not saying you should, just make sure you are happy which is the

    best way
    > for you.
    >
    > --
    >
    > HTH
    >
    > Bob Phillips
    > ... looking out across Poole Harbour to the Purbecks
    > (remove nothere from the email address if mailing direct)
    >
    > "Paul Simon" <> wrote in

    message
    > news:...
    > > Thanks very much Bob. Your first paragraph was the clue to my

    problem. I
    > changed from Hide to Unload Me, and now it works perfectly.

    Thanks again,
    > Bob - I appreciate it.
    > >> Paul

    > >
     
    Bob Phillips, Feb 11, 2004
    #6
  7. Guest

    Guest Guest

    Bob,

    Terrific! Thanks again so very much for all the time and help you've given me on this - it's very much appreciated. And I'll certainly take into account your caution about being careful to avoid not being able to close the form. (I'll do my experimenting on a "Test" file first.)

    Many, many thanks,
    Paul


    ----- Bob Phillips wrote: -----

    Paul,

    As I think you suspected, it is unloaded at this point.

    Good news though, there is a QueryClose event that is invoked whenever the
    form is unloaded. There are 4 close circumstances, so you can trap them and
    react as you see fit. Here is some code

    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    Select Case CloseMode
    Case vbFormControlMenu:
    Cancel = True 'The user has chosen the Close command from the
    Control menu on the UserForm.
    Case vbFormCode:
    'The Unload statement is invoked from code.
    Case vbAppWindows:
    'The current Windows operating environment
    session is ending.
    Case vbAppTaskManager:
    'The Windows Task Manager is closing the
    application
    End Select

    End Sub

    In this I have shown all 4 instabces, but the only one that is trated is the
    Control Menu close, the X that is, where I cancel the close. So it has no
    effect to click the X.

    You could action some of the others, but be careful, you might never be able
    to close the dang thing.

    --

    HTH

    Bob Phillips
    ... looking out across Poole Harbour to the Purbecks
    (remove nothere from the email address if mailing direct)

    "Paul Simon" <> wrote in message
    news:...
    > Hi Bob,
    >> I have a follow-up question. First of all, your suggestion of my putting

    my Userform1.Textbox1.SetFocus line in the Userform_Activate event and
    changing the code in my Exit button back to Userform1.Hide (instead of
    Unload Me) all works perfectly.
    >> What I'm wondering is this: If the user clicks the Close button (the

    built-in "x" button at the top right corner of the Userform) instead of my
    Exit button, is the Userform being hidden or unloaded? If unloaded, is
    there a way to remove the "x" Close button from the Userform?
    >> Many thanks,

    > Paul
    >> ----- Paul Simon wrote: -----
    >> Hi Bob,
    >> You are absolutely right! If I load and unload the form several

    times, I eventually run out of memory. In fact, I even reached a point
    where Excel told me it could not save the file. (And exiting Excel does not
    regain the memory. You actually have to reboot.)
    >> Thanks very much for great advice, Bob.
    >> Paul
    >>>>> ----- Bob Phillips wrote: -----

    >> Paul,
    >> Could I advise you to re-consider. Sometimes, often, it is

    better to hide a
    > sheet rather than unload it, thereby removing that load overhead

    if you
    > re-load in the same session. You can still achieve your

    objective using the
    > Activate event.
    >> Not saying you should, just make sure you are happy which is the

    best way
    > for you.
    >> --
    >> HTH
    >> Bob Phillips

    > ... looking out across Poole Harbour to the Purbecks
    > (remove nothere from the email address if mailing direct)
    >> "Paul Simon" <> wrote in

    message
    > news:...
    >> Thanks very much Bob. Your first paragraph was the clue to my

    problem. I
    > changed from Hide to Unload Me, and now it works perfectly.

    Thanks again,
    > Bob - I appreciate it.
    >>> Paul

    >>
     
    Guest, Feb 11, 2004
    #7
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Zane Greer

    UserForm1.Textbox1.SetFocus Question

    Zane Greer, Sep 14, 2003, in forum: Microsoft Excel Programming
    Replies:
    1
    Views:
    1,206
    Bob Phillips
    Sep 14, 2003
  2. Guest

    Setfocus won't work on a userform's textbox control

    Guest, May 24, 2004, in forum: Microsoft Excel Programming
    Replies:
    4
    Views:
    759
    Guest
    May 29, 2004
  3. Zoo
    Replies:
    1
    Views:
    329
    Wendell A. Clark
    May 11, 2006
  4. Jim May

    UserForm1 Not Displaying Textbox Content

    Jim May, Aug 3, 2006, in forum: Microsoft Excel Programming
    Replies:
    4
    Views:
    169
    Jim May
    Aug 3, 2006
  5. chuckm

    Running UserForm1 routines from UserForm 2

    chuckm, Feb 12, 2010, in forum: Microsoft Excel Programming
    Replies:
    4
    Views:
    408
    Chip Pearson
    Feb 12, 2010
Loading...

Share This Page