Results 1 to 5 of 5

Thread: [RESOLVED] why cant we .setfocus on formload?

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2008
    Posts
    47

    Resolved [RESOLVED] why cant we .setfocus on formload?

    Private Sub Form_Load()
    Me.txt_rno.SetFocus

    End Sub


    why this fails?

  2. #2
    Hyperactive Member
    Join Date
    Oct 2007
    Location
    Indiana
    Posts
    295

    Re: why cant we .setfocus on formload?

    It fails because the entire form must load before it can be used.

    Put your set focus code in the form's Activate Event, not in it's Load Event.

    The Activate Event runs immediately after the load event.

    Code:
    Private Sub Form_Activate()
       Me.txt_rno.SetFocus
    End Sub
    Last edited by Caskbill; Jan 30th, 2008 at 11:39 AM.

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [RESOLVED] why cant we .setfocus on formload?

    In short, all of the code in the Form Load will execute before the form becomes visible.

    So, in effect, you are attempting to set focus to a control that is not visible, and that will throw an error.

    If you need this control to have the focus when the form opens, then the easiest way, without any code at all, is to set its TabIndex property to 0

  4. #4
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: [RESOLVED] why cant we .setfocus on formload?

    This will probably work too:
    The last line in form load
    me.show or me.visible = True
    text1.setfocus

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [RESOLVED] why cant we .setfocus on formload?

    Because of this
    Quote Originally Posted by Hack
    In short, all of the code in the Form Load will execute before the form becomes visible.
    Me.Visible would never evaulate to True so the statement wouldn't run anyway.

Posting Permissions

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



Click Here to Expand Forum to Full Width