Solved

Requesting Focus to a Text Box

Posted on 1998-07-29
6
143 Views
Last Modified: 2011-10-03
I have a main splash screen with a timer (frmSplash) that opens a form called frmLogin.  In the frmLogin Load() method, I call txtUserName.setFocus, but I get an error message.


Private Sub Form_Load()
    txtUserName.SetFocus
End Sub


My error is:

Runtime error '5'

Invalid procedure call or argument
0
Comment
Question by:kpetti
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 9

Accepted Solution

by:
Dalin earned 20 total points
ID: 1467161
kpetti,
put the code in form_activate instead of form_load.
Regards
Dalin
0
 

Author Comment

by:kpetti
ID: 1467162
It is really strang, but when I took the code:

Private Sub Form_Load()
    txtUserName.SetFocus
End Sub

out of my form, it worked fine.  I must not have like the above code for some reason or another.  I think that it may be becuase it was having a conflict with the tab index = 0 on that textfield.

Kev
0
 
LVL 9

Expert Comment

by:Dalin
ID: 1467163
Well, Form_load is when it "Load everything in the form. So your textbox may not be there yet...
form_activate is the event after form_load, so everything is there now...
All these happens before you see them.

The other way of seting focus for a control is to set its tabIndex property to 0.

Regards
Dalin
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 39

Expert Comment

by:abel
ID: 1467164
Kev,
You are only getting that error because your form doesn't show yet when inside the form_load event. To explain you when and why you can use setfocus, this is how your form gets created (from first to last event):
form_initialize
form_load
form_resize
form_activate
form_paint

Mostly people use form_initialize, form_load and form_activate for all kinds of initialization. form_load also occurs when you refer to any object to it before it's displayed (like text1.text="hello" also calls form_load).
Although you can use SetFocus from the form_resize event, it's better to use it, like Dalin says, from form_activate (that's what it should be used for)

Another solution (many people use it, but it's not so beautiful): put me.show before you use SetFocus, like:
Private Sub Form_Load()
    Me.Show
    txtUserName.SetFocus
End Sub

This all has nothing to do with the tabstop or tabindex properties.

Regards, Abel
0
 
LVL 39

Expert Comment

by:abel
ID: 1467165
Sorry Dalin, I was typing when you already posted... :-)
0
 
LVL 9

Expert Comment

by:Dalin
ID: 1467166
Thanks, abel, for the complete explination. I am sure Kev benifited from that.
Regards
Dalin
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question