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

Control\tab Index help

Status
Not open for further replies.

scoobey

Technical User
Sep 18, 2001
32
GB
Hi!

I am pulling my hair out over this one! Basically, I want the first control in my form (or the field that has a tab index of 0) to automatically receive the focus when a user clicks on a button. All fields are disabled by default, but when a user clicks on the 'Edit' button, all the fields are enabled so that the user can edit changes. The only problem is that all my form functions are automated using modules and I can't seem to get the first control on a form (or a field with a tab order of 0) to be given the focus, without specifying its name! I expect something below should work, but i'm not sure of the exact syntax.

Me.Controls(0).SetFocus ????
Me.TabIndex(0).SetFocus ????

Any help would be MUCH appreciated - thanks!
 
A crude way:
For i = 0 To Me.Controls.Count - 1
If Me.Controls(i).TabIndex = 0 Then
Me.Controls(i).SetFocus
Exit For
End If
Next

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks for responding, PH. The only problem is that I don't think the control property can reference the TabIndex value as it returns an error and highlights the "If Me.Controls(i).TabIndex = 0 Then" line:

Run-time error '438':
Object doesn't support this property or method.

Any other suggesstions? Thanks!
 
A less crude way:
For i = 0 To Me.Controls.Count - 1
With Me.Controls(i)
Select Case .ControlType
Case acCheckBox, acComboBox ,acListBox ,acTextBox
If .TabIndex = 0 Then
.SetFocus
Exit For
End If
End Select
End With
Next

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi scoobey,

Perhaps this is what you are looking for:

Dim ctl As Control

Set ctl = Me.Controls(0)
ctl.SetFocus

This will not go to the TabIndex(0) as you are looking for though. However I also use a module to disable/enable controls in a form. One trick I use is to create a box and shrink it completely so that it is invisible and set its TabIndex to the number right before the control you want to give the focus to.
Call it the same name on each form and once the 'Edit' button is pressed give it the focus and then tab to the next control and this should give your desired control the focus:

Private Sub cmdEdit_OnClick()

Call the Enable/Disable Module

Me.boxFocus.setfocus
SendKeys "{TAB}"

End Sub

Hope this helps. Let me know

Lawrence
 
Excellent - works a treat! Thanks chaps!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top