Solved

Active control

Posted on 2001-07-05
17
192 Views
Last Modified: 2012-06-27
Dear Friends,

 i have to find out the last active control on the form how to do that any function is available to find out that. Like suppose i am entering values in Text1 after pressing tab the cursor goes to next control i.e. Text2
now in Text2.GotFocus() event i want to find out the last
active control how to do that. In this case Text1 is the last active control.

Plz help me.

regards.
Jay
0
Comment
Question by:hijay23
  • 3
  • 3
  • 2
  • +7
17 Comments
 
LVL 11

Accepted Solution

by:
Otana earned 50 total points
ID: 6255486
you could put this in the lostfocus event of each control:

LastControl= Name-Of-Control

and make LastControl a public variable

But if you have many controls, it's quite a job to do so.
0
 
LVL 3

Expert Comment

by:nigelrowe
ID: 6255550
Maybe you could set a global variable to the name of the control which has just lost the focus, something like...


Option Explicit
Private lastfocus As String

Private Sub Text1_GotFocus()
    MsgBox lastfocus
End Sub
Private Sub Text2_GotFocus()
    MsgBox lastfocus
End Sub

Private Sub Text1_LostFocus()
    lastfocus = "Text1"
End Sub
Private Sub Text2_LostFocus()
    lastfocus = "Text2"
End Sub
0
 
LVL 3

Expert Comment

by:nigelrowe
ID: 6255558
It's one off those days!!!!
0
 

Author Comment

by:hijay23
ID: 6255649
No i don't want to do this i want any readymade function or API call.
0
 
LVL 3

Expert Comment

by:nigelrowe
ID: 6255747
I don't think you've got much choice here, but I might be mistaken.
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 6255757
There isn't a ready made function for this. Each control is autonomous within its container it is only responsible for events which occur whilst it is getting/has/losing focus you CANNOT infer the previous control from the current one in any way as the current control does not even know about the existence of any other controls unless it is bound to them in some fashion. You HAVE to follow one of the methods already suggested in some way in order to achieve your desired result.
0
 
LVL 7

Expert Comment

by:Inteqam
ID: 6255803
why do you want such a thing, maybe there is an alternative for what you are trying to do.
0
 
LVL 8

Expert Comment

by:Dave_Greene
ID: 6255824
You could try looping through each control on the container and pull out the one with the highest tabindex

For each ctl in form1.controls
  if ctl.tabindex > highindex then
     highindex = ctl.tabindex
     highname  = ctl.name
  end iff
next  
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 7

Expert Comment

by:Inteqam
ID: 6255834
the highest but less than the current control tab index , but this wont work if you used a shortcut or mouse.
0
 
LVL 11

Expert Comment

by:Otana
ID: 6255858
yes and if you use tab, why don't you just take the current tab index -1 ?
0
 
LVL 7

Expert Comment

by:Inteqam
ID: 6255873
lol, right


always think simple.
:o)

but i think there can be a gab between, i mean you can have two controls only on a form , one with 0 tab index and the other with a 4 tab index ( i am not sure). :o(
0
 
LVL 4

Expert Comment

by:nutwiss
ID: 6256144
hijay - what are you trying to do? sounds like a validation problem
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 6256294
(From MS Help)
ActiveControl Property

Returns the control that has thefocus. When a form is referenced, as in ChildForm.ActiveControl, ActiveControl specifies the control that would have the focus if the referenced form were active. Not available atdesign time; read-only atrun time.

--
However, I'm not sure how you would implement something like this in your case without creating an "endless" loop that checks for it.
After all, as soon as you click on something else, the control loses focus and is no longer active!
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 6312040
hijay23, please follow up on this question.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7148107
Hi hijay23,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Split points between: Otana and TimCottee

hijay23, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
0
 
LVL 5

Expert Comment

by:Netminder
ID: 7182120
Per recommendation, force-accepted.

Netminder
CS Moderator

TimCottee: points for you at http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20328902
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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.
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…
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…

706 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

20 Experts available now in Live!

Get 1:1 Help Now