Solved

Requesting Focus to a Text Box

Posted on 1998-07-29
6
138 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
  • 3
  • 2
6 Comments
 
LVL 9

Accepted Solution

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

Author Comment

by:kpetti
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 39

Expert Comment

by:abel
Comment Utility
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
Comment Utility
Sorry Dalin, I was typing when you already posted... :-)
0
 
LVL 9

Expert Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now