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
Solved

Requesting Focus to a Text Box

Posted on 1998-07-29
6
141 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

840 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