Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Setting Focus of a Control

Posted on 2006-11-02
9
Medium Priority
?
184 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 143

Accepted Solution

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

controls(StrControlName).SetFocus
0
 
LVL 17

Assisted Solution

by:inthedark
inthedark earned 400 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 400 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
LVL 143

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 143

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…

971 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