Solved

Determine which control has focus/active/clicked

Posted on 2011-02-17
4
513 Views
Last Modified: 2013-11-28
I have a continuous form that has 48 text boxes (2 per hour) and a total of 4 rows. I want to know how to determine which of those boxes was clicked. When you click the box it opens up a popup form with a combo box. The value of the combo box is then passed back to the box that was clicked.

Maybe you need to look at it to see what is going on. It works if I program each and every textbox, but I am trying to 'automate' it a bit better.

Thanks for taking a look. MoldChanges.accdb

P.S. I have tried something like this on a few form events: Dirty, After_Update, Current. None of those worked.

Dim strActiveCtl As String

strActiveCtl = Screen.ActiveControl.Name

MsgBox strActiveCtl

And I may need to know how to do something like Forms!FormName! & Variable.  If that is possible.
0
Comment
Question by:G Scott
[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
  • 2
4 Comments
 
LVL 1

Author Comment

by:G Scott
ID: 34921739
P.S. I have tried something like this on a few form events: Dirty, After_Update, Current. None of those worked.

Dim strActiveCtl As String

strActiveCtl = Screen.ActiveControl.Name

MsgBox strActiveCtl
0
 
LVL 85

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 250 total points
ID: 34921968
If you MUST know which control is active, you'll have to include code in the GotFocus or Enter event of every control to set your module level variable. Screen.ActiveControl will tell you which is active, but that may or may not be what you expect. The only way to be sure is to code in the control events - not the form event.
0
 
LVL 14

Assisted Solution

by:pteranodon72
pteranodon72 earned 250 total points
ID: 34929214
If you want all 48 boxes to behave the same way on a click, you need to put similar code in the event of all 48 boxes. VB classic allowed control arrays on a form, but that functionality is not available in Access forms. You must write separate code.

You can reduce the burden of writing 48 routines (and especially modifying all 48 routines) by having them all call the same function (must be a function, not a sub) and pass their own control name.

'funtion within form's module:
Public Function ReactToClick(strCaller As String)
Me(strCaller) = InputBox("What do you want to put in box " & strCaller)
End Function

Then, select all 48 textboxes, open the property window, find the space for On Click on the Events tab, and type:

=ReactToClick("")

Next (and this is what you dreaded), select the textboxes one-at-a-time and insert the name of the textbox or name of the bound field between the double quotes. This makes every event able to reach the control through the Me(controlName) construct.

Unfortunately, VBA does not have a construction like "this" in JavaScript or Java. The closest you get is Me, which points to the current form or report.

Hope this helps,

pT72
0
 
LVL 1

Author Closing Comment

by:G Scott
ID: 34931094
Thanks. You guys pointed me in the right direction. I used a bit of:

Public Function controlName(thecontrol As String)
Dim currentControl As String
Dim currentForm As String
currentControl = Screen.ActiveControl.Name
currentForm = Screen.ActiveControl.Parent.Name
DoCmd.OpenForm "Form1"
Forms!Form1!txtControlName = currentControl
Forms!Form1!txtCurrentForm = currentForm
Forms!Form1!txtRow = Forms!Form2(currentForm)!Row


Where I would call that fuction by, like you said pteranodon72, putting the control name on each and every call. Tedious, but it worked. Only to find out it really isn't a valid solution after all. Guess I need to really think it all the way through.

Thanks for the help. I learned something new. :)
0

Featured Post

Industry Leaders: 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

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
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…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

688 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