?
Solved

Determine which control has focus/active/clicked

Posted on 2011-02-17
4
Medium Priority
?
517 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 750 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 750 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

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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

762 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