We help IT Professionals succeed at work.

Setting Focus of a Control

LFMSupport
LFMSupport asked
on
Medium Priority
195 Views
Last Modified: 2010-04-30
Quick one. Is there anyway of setting the focus of a control held in a string.

e.g

Dim StrControlName as String

StrControlName.SetFocus

or

Set ActiveControl = StrControlName

The above 2 dont work obviously but is there any other way without using some fancy API?

I need this quick so theres 500 at stake!!

Thanks.
Comment
Watch Question

Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009
Commented:
Dim StrControlName as String

controls(StrControlName).SetFocus

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT
Commented:
Expanding on angelii's solution the controls collection is one of the Forms property so within a form the full notation would be:

Me.Controls("MyControlName").Setfocus

Or form outside of the fomr's code module

frmMyForm.Controls("MyControl").Setfocus

You also have to consider also that you may also be using a control arrays.

Further you shoul always place this code in a subroutine becuase you always need to set an error trap (you don;t want yor code to crash.  If the control is not visible then it will fail and cause a crash.  Why would the control not be visible (say the operator minimizes the form) or you set the control invisible for some reason.

SetFocus Me.Controls("MyControlName")

Public Sub SetFocus(pControl As Control)
On Error Resume Next
pControl.Setfocus
End Sub

Hope this helps:~)
Shiju SasidharanSenior IT Developer
Commented:
'Usage
'            SetControlFocus "Command1"
'            SetControlFocus "Text1"


Private Sub SetControlFocus(Cntrl As String)
Dim Cntr As Control
    Set Cntr = CallByName(Me, Cntrl, VbGet)
    If Cntr.Enabled And Cntr.Visible Then
        Cntr.SetFocus
    End If
End Sub

Author

Commented:
Sorry, couldnt get any of the above working.

Type mismatch error!

I have found a suitable workaround but will still award points if answered correctly.

Workaround is to store TabIndex of control in a string then use ActiveControl.TabIndex = CInt(String)

Thanks.
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
>Type mismatch error!

I could not reproduce a type mismatch, but only a "invalid procedure call", and that only when the form was not visible (fully loaded)...

Author

Commented:
Actually im getting "Object does not support this property or method" not type mismatch.

Author

Commented:
Thats exaclty what im using:

Private Sub Test()

    Dim str As String
   
    str = "txtSecAddress"

    Controls(str).SetFocus

End Sub
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
>Object does not support this property or method

well, you might have a control that indeed does not support the SetFocus method, or is possibly disabled at the time you call the SetFocus method.

Author

Commented:
Doh, that control is locked!!

Thanks for your help mate.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.