Setting Focus of a Control

Quick one. Is there anyway of setting the focus of a control held in a string.


Dim StrControlName as String



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!!

Who is Participating?
Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:
Dim StrControlName as String

inthedarkConnect With a Mentor Commented:
Expanding on angelii's solution the controls collection is one of the Forms property so within a form the full notation would be:


Or form outside of the fomr's code module


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
End Sub

Hope this helps:~)
Shiju SasidharanConnect With a Mentor Assoc Project ManagerCommented:
'            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
    End If
End Sub
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

LFMSupportAuthor 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)

Guy Hengel [angelIII / a3]Billing EngineerCommented:
>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)...
LFMSupportAuthor Commented:
Actually im getting "Object does not support this property or method" not type mismatch.
LFMSupportAuthor Commented:
Thats exaclty what im using:

Private Sub Test()

    Dim str As String
    str = "txtSecAddress"


End Sub
Guy Hengel [angelIII / a3]Billing EngineerCommented:
>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.
LFMSupportAuthor Commented:
Doh, that control is locked!!

Thanks for your help mate.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.