Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SetFocus

Status
Not open for further replies.

savok

Technical User
Jan 11, 2001
303
0
0
AT
Ok when i set TxtName.Setfocus in a command button it works just fine but when I put into form load it gives an error? Can anyone plz tell me what i am doing wrong

Private Sub cmdAdd_Click(Index As Integer)
txtLastName.SetFocus
End Sub

the above works fine but the below doesnt

Private Sub Form_Load()
txtLastName.SetFocus
End Sub


thanks in advance
 
What is the Error Message?
Clayton T. Paige
cpaige@home.com
Clayton T. Paige

Programmer Extraordinaire

========================================================

"Who is General Failure? and Why is he reading my disk drive?"
 
I can't tell you how to fix it because I haven't got VB on this machine to test it. But I believe it maybe that the txtLastName object isn't loaded when the sub calls it. Try it in another sub like Form_Initalise or something. Sorry I can't be more help but it works fine in VBA.
 
The error msg is "Invalid procedure call or argument"

I tried using this in Form_Initialize and it also gives the same error. Any other ideas? :) thanks
 
Try typing:

Msgbox txtLastName.Text

in the sub to find out if the object is loaded.

I've had this problem before, I can find out tonight how to solve it.
 
The msg box works just fine, weird

I tried making the form show before setting focus, I do not get the error msg but the focus doesnt not get set

Private Sub Form_Load()
MsgBox txtLastName.Text
frmStudentSearch.Show
txtLastName.SetFocus
End Sub

Gets rid of the error but the focus isnt set :(

Thanks again
 
I just realized something, could it have something to do with the fact that I am opening the form as a child of another?
 
Ahh got it ;) Had to set it right after I open it. Thanks guys :)

Private Sub mnuSearch_Click()
frmStudentSearch.Show
SetParent frmStudentSearch.hWnd, frmMain.hWnd
frmStudentSearch.txtLastName.SetFocus
End Sub
 
You cannot use the SetFocus method at all in a Form_Load because no controls are loaded yet. You must use the Tab Order to accomplish this.
 
Another way to do it is to put it in the Form_Activate event. This will get it focus immediately.
 
An object can only Get the Focus if it is Visible and Enabled.
Therefore you can't use the SetFocus from within a Form's load event, unless you first use the Show method (which can be put near the End of the Load Event). _________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Please help me!

I have an usercontrol in ocx project. I have a standard exe project (Main). Is there a form (frmCtrl) in main that contain this usercontrol from ocx project. The frmMain form is the main screen for main exe. When I reparent (API:Setparent) the frmCtrl picturebox (which contain this usercontrol) into a picbox of the frmMain the usercontrol is out of focus in runtime. The tabindex is not working... :((

I would like dynamically load lot of UI component for menu items. Menu items loaded from database. Each menu items contain a text for screen, and text for ProgID which will be loaded. The "form.controls.add(progID,...)" function has memory leak on Win9x platforms... :(((

Please help!

thx, any idea:
Dani

P.S: sorry my poor english!
 
To Kovesdan (Dani)

I suggest you repost this question in a new
message thread so that more people will see it.
Make sure you give it a descriptive title too.

good luck :)
 
I get the same problem. But it seems to work fine in the design enveronment. Then when you get to the client the error jumps up and there is no way to error trap it. I believe that either you have to check if the control in enabled and visible first.

ie if ctrl.visible and ctrl.enabled then ctrl.setfocus

or I sometimes get the problem if I place the Set Focus in a sub procedure 1 or more levels below the event.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top