Solved

Requesting Focus to a Text Box

Posted on 1998-07-29
6
145 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Industry Leaders: 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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

705 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