Solved

Requesting Focus to a Text Box

Posted on 1998-07-29
6
139 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

861 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

23 Experts available now in Live!

Get 1:1 Help Now