Solved

Determine which control has focus/active/clicked

Posted on 2011-02-17
4
478 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
  • 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 84

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

920 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

16 Experts available now in Live!

Get 1:1 Help Now