• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 187
  • Last Modified:

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

3 Solutions
Guy Hengel [angelIII / a3]Billing EngineerCommented:
Dim StrControlName as String

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 SasidharanAssoc 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
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now