Solved

Setting Focus of a Control

Posted on 2006-11-02
9
171 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.
0
Comment
Question by:LFMSupport
9 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 300 total points
ID: 17856777
Dim StrControlName as String

controls(StrControlName).SetFocus
0
 
LVL 17

Assisted Solution

by:inthedark
inthedark earned 100 total points
ID: 17857022
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:~)
0
 
LVL 14

Assisted Solution

by:Shiju Sasidharan
Shiju Sasidharan earned 100 total points
ID: 17857264
'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
0
 
LVL 1

Author Comment

by:LFMSupport
ID: 17857680
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.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17857734
>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)...
0
 
LVL 1

Author Comment

by:LFMSupport
ID: 17857775
Actually im getting "Object does not support this property or method" not type mismatch.
0
 
LVL 1

Author Comment

by:LFMSupport
ID: 17857784
Thats exaclty what im using:

Private Sub Test()

    Dim str As String
   
    str = "txtSecAddress"

    Controls(str).SetFocus

End Sub
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17857823
>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.
0
 
LVL 1

Author Comment

by:LFMSupport
ID: 17857873
Doh, that control is locked!!

Thanks for your help mate.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now