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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

.SetFocus Problem 1

Status
Not open for further replies.

dwilko

Technical User
Feb 20, 2002
113
GB
I am Writing a program and I want it to setfocus on a text box when the form loads. when I run the program it gives me a invalid Procedure call or argument.
the text box it called "txtReg"

here is my code
Code:
Private Sub Form_Load()
   
   txtReg.SetFocus
        
End Sub

can anyone tell me what is wrong with this code and how I can fix it so that it sets the focus on the text box when the form loads.

Thanks


David

________________________________________
I help Where I can, If My advice Dont work Or Is Wrong then tell Me
 
Try using Form_Activate() instead of Form_Load() and the same textbox.setfocus.
 
It worked

Thanks for your help

________________________________________
I help Where I can, If My advice Dont work Or Is Wrong then tell Me
 
You could also simply set the TabIndex of the textbox to 0.
 
Actually, it would be a good idea to place the .setfocus call in the form's initilize event, because the activate event might fire again when you don't expect it. In this case, it's probably less important, but it is often the case that code placed within the form_activate might be fired un-intentionally.

[fish] No Dolphins were harmed in the posting of this message... Dolphin Friendly Tuna!

Ever feel like you're banging your head against a tree? I did, so I cut down the tree.
 
Normally the Initialise event occurs before the load event. The form doesn't actually get shown until the end of the Load event, and you can't do a SetFocus until the Form is populated.

The tidy way of doing this on first load and not on every successive Activate is to force the form to show first. Put this at the end of the Form_Load event:

Me.Show
Text5.SetFocus


________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
johnwm, is there any reason that simply setting the textbox tabindex to 0 shouldn't be used? If it is set to 0 then on the form load your focus will be set to that textbox. I am curious as to whether or not this is a bad way to do this. Please advise and thanks for all the help you have been providing in this forum.....i have learned much.
 
bmdb

No reason at all why setting TabIndex to 0 won't work. If it's only the first SetFocus issue you're concerned with, then that's good.

However if you want to set the initial focus to different controls depending on where in the program the form is called from, then using a TabIndex setting will upset the rest of the TabOrder, while SetFocus doesn't disturb TabIndex

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
Every now and then, I make myself look like a total idiot. Thanks John for correcting me. I don't know what the H3ll I was thinking with that one.

[fish] No Dolphins were harmed in the posting of this message... Dolphin Friendly Tuna!

Ever feel like you're banging your head against a tree? I did, so I cut down the tree.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top