Solved

Setting Focus of a Control

Posted on 2006-11-02
9
181 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 143

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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month3 days, 23 hours left to enroll

630 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